arcan-lago 0.3.0

Bridge between Arcan agent runtime and Lago event-sourced persistence
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
//! Promotion Gate — policy-gated rule promotion with rollback.
//!
//! Phase 2.3 of the self-learning pipeline. Takes `RuleProposal` entries
//! produced by the consolidation job (Phase 2.2), validates them against
//! policy, optionally routes through the approval gate, and commits them
//! as active rules. Supports full rollback via `learning.reverted` events.
//!
//! Event types emitted (all `EventPayload::Custom` with `"learning."` prefix):
//! - `learning.promoted`  — proposal accepted, rule now active
//! - `learning.rejected`  — proposal explicitly rejected (with reason)
//! - `learning.reverted`  — previously promoted rule rolled back

use lago_core::event::{EventEnvelope, EventPayload, PolicyDecisionKind};
use lago_core::id::{BranchId, EventId, SessionId};
use lago_core::projection::Projection;
use lago_core::{Journal, LagoError, LagoResult};
use lago_policy::PolicyEngine;
use lago_store::BlobStore;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

// ── Event type constants ────────────────────────────────────────────

pub mod event_types {
    /// Emitted when a rule proposal is promoted to an active rule.
    pub const LEARNING_PROMOTED: &str = "learning.promoted";
    /// Emitted when a rule proposal is explicitly rejected.
    pub const LEARNING_REJECTED: &str = "learning.rejected";
    /// Emitted when a previously promoted rule is rolled back.
    pub const LEARNING_REVERTED: &str = "learning.reverted";
}

// ── Types ───────────────────────────────────────────────────────────

/// What a promoted rule does when it takes effect.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type", content = "value")]
pub enum ProposalAction {
    /// Inject guidance text into the system prompt's Rules block.
    AddSystemPromptGuidance(String),
    /// Add a policy rule to the runtime policy engine.
    AddPolicyRule {
        rule_id: String,
        name: String,
        priority: u32,
        condition_json: serde_json::Value,
        decision: String,
        explanation: Option<String>,
    },
    /// Modify a tool's runtime configuration.
    ModifyToolConfig {
        tool_name: String,
        config_patch: serde_json::Value,
    },
}

/// A rule proposal produced by the consolidation job (Phase 2.2).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleProposal {
    /// Unique identifier for this proposal.
    pub proposal_id: String,
    /// Human-readable description of the proposed rule.
    pub description: String,
    /// The action to take if this proposal is promoted.
    pub action: ProposalAction,
    /// Confidence score from the consolidation job (0.0–1.0).
    pub confidence: f64,
    /// Source learning entries that led to this proposal.
    pub source_learning_ids: Vec<String>,
    /// Session where the proposal was generated.
    pub session_id: String,
}

/// Configuration for the promotion gate.
#[derive(Debug, Clone)]
pub struct PromotionConfig {
    /// If true, proposals below `auto_promote_confidence` require human approval.
    pub require_human_approval: bool,
    /// Confidence threshold for automatic promotion (default: 0.95).
    pub auto_promote_confidence: f64,
    /// Maximum number of active rules (safety cap, default: 50).
    pub max_active_rules: usize,
    /// Cooldown period after a revert before the same proposal can be re-promoted.
    pub cooldown_after_revert: Duration,
}

impl Default for PromotionConfig {
    fn default() -> Self {
        Self {
            require_human_approval: true,
            auto_promote_confidence: 0.95,
            max_active_rules: 50,
            cooldown_after_revert: Duration::from_secs(24 * 60 * 60),
        }
    }
}

/// A rule that has been promoted from a proposal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotedRule {
    /// Unique ID for this active rule (generated on promotion).
    pub rule_id: String,
    /// The proposal that was promoted.
    pub proposal_id: String,
    /// Human-readable rule text.
    pub rule_text: String,
    /// The action this rule enacts.
    pub action: ProposalAction,
    /// Journal sequence number at promotion time.
    pub promoted_at: u64,
    /// Whether this rule has been reverted.
    pub reverted: bool,
}

/// An event in the promotion audit trail.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotionEvent {
    pub event_type: String,
    pub rule_id: Option<String>,
    pub proposal_id: String,
    pub reason: Option<String>,
    pub timestamp: u64,
    pub seq: u64,
}

/// Error types specific to the promotion gate.
#[derive(Debug, thiserror::Error)]
pub enum PromotionError {
    #[error("proposal {0} not found")]
    ProposalNotFound(String),
    #[error("rule {0} not found")]
    RuleNotFound(String),
    #[error("rule {0} already reverted")]
    AlreadyReverted(String),
    #[error("active rule limit reached ({0})")]
    ActiveRuleLimitReached(usize),
    #[error("policy conflict: {0}")]
    PolicyConflict(String),
    #[error("proposal {0} is in cooldown after revert")]
    CooldownActive(String),
    #[error("requires human approval")]
    RequiresApproval,
    #[error("lago error: {0}")]
    Lago(#[from] LagoError),
}

// ── Active Rule Set Projection ──────────────────────────────────────

/// Projection that rebuilds the set of active promoted rules by folding
/// over `learning.promoted` and `learning.reverted` events.
#[derive(Debug, Default, Clone)]
pub struct ActiveRuleSet {
    rules: Vec<PromotedRule>,
    history: Vec<PromotionEvent>,
    /// Tracks reverted proposal IDs with their revert timestamp (micros).
    reverted_proposals: HashMap<String, u64>,
}

impl ActiveRuleSet {
    pub fn new() -> Self {
        Self::default()
    }

    /// Get all currently active (non-reverted) rules.
    pub fn active_rules(&self) -> Vec<&PromotedRule> {
        self.rules.iter().filter(|r| !r.reverted).collect()
    }

    /// Count of active (non-reverted) rules.
    pub fn active_count(&self) -> usize {
        self.rules.iter().filter(|r| !r.reverted).count()
    }

    /// Get the full promotion history (all events).
    pub fn history(&self) -> &[PromotionEvent] {
        &self.history
    }

    /// Check if a proposal was recently reverted (within cooldown).
    pub fn is_in_cooldown(&self, proposal_id: &str, cooldown: Duration, now_micros: u64) -> bool {
        if let Some(&revert_time) = self.reverted_proposals.get(proposal_id) {
            let cooldown_micros = cooldown.as_micros() as u64;
            return now_micros.saturating_sub(revert_time) < cooldown_micros;
        }
        false
    }

    /// Find a promoted rule by rule_id.
    pub fn find_rule(&self, rule_id: &str) -> Option<&PromotedRule> {
        self.rules.iter().find(|r| r.rule_id == rule_id)
    }

    /// Fold a single event into the projection.
    pub fn fold(&mut self, event: &EventEnvelope) {
        if let EventPayload::Custom {
            ref event_type,
            ref data,
        } = event.payload
        {
            match event_type.as_str() {
                event_types::LEARNING_PROMOTED => {
                    if let (Some(rule_id), Some(proposal_id), Some(rule_text)) = (
                        data.get("rule_id").and_then(|v| v.as_str()),
                        data.get("proposal_id").and_then(|v| v.as_str()),
                        data.get("rule_text").and_then(|v| v.as_str()),
                    ) {
                        let action: Option<ProposalAction> = data
                            .get("action")
                            .and_then(|v| serde_json::from_value(v.clone()).ok());

                        self.rules.push(PromotedRule {
                            rule_id: rule_id.to_string(),
                            proposal_id: proposal_id.to_string(),
                            rule_text: rule_text.to_string(),
                            action: action.unwrap_or(ProposalAction::AddSystemPromptGuidance(
                                rule_text.to_string(),
                            )),
                            promoted_at: event.seq,
                            reverted: false,
                        });

                        self.history.push(PromotionEvent {
                            event_type: event_types::LEARNING_PROMOTED.to_string(),
                            rule_id: Some(rule_id.to_string()),
                            proposal_id: proposal_id.to_string(),
                            reason: None,
                            timestamp: event.timestamp,
                            seq: event.seq,
                        });
                    }
                }
                event_types::LEARNING_REJECTED => {
                    if let Some(proposal_id) = data.get("proposal_id").and_then(|v| v.as_str()) {
                        self.history.push(PromotionEvent {
                            event_type: event_types::LEARNING_REJECTED.to_string(),
                            rule_id: None,
                            proposal_id: proposal_id.to_string(),
                            reason: data
                                .get("reason")
                                .and_then(|v| v.as_str())
                                .map(String::from),
                            timestamp: event.timestamp,
                            seq: event.seq,
                        });
                    }
                }
                event_types::LEARNING_REVERTED => {
                    if let Some(rule_id) = data.get("rule_id").and_then(|v| v.as_str()) {
                        // Mark the rule as reverted
                        for rule in &mut self.rules {
                            if rule.rule_id == rule_id {
                                rule.reverted = true;
                                // Track revert for cooldown
                                self.reverted_proposals
                                    .insert(rule.proposal_id.clone(), event.timestamp);
                            }
                        }

                        self.history.push(PromotionEvent {
                            event_type: event_types::LEARNING_REVERTED.to_string(),
                            rule_id: Some(rule_id.to_string()),
                            proposal_id: data
                                .get("proposal_id")
                                .and_then(|v| v.as_str())
                                .unwrap_or("")
                                .to_string(),
                            reason: data
                                .get("reason")
                                .and_then(|v| v.as_str())
                                .map(String::from),
                            timestamp: event.timestamp,
                            seq: event.seq,
                        });
                    }
                }
                _ => {}
            }
        }
    }
}

impl Projection for ActiveRuleSet {
    fn on_event(&mut self, event: &EventEnvelope) -> LagoResult<()> {
        self.fold(event);
        Ok(())
    }

    fn name(&self) -> &str {
        "arcan::active_rules"
    }
}

// ── Promotion Gate ──────────────────────────────────────────────────

/// Policy-gated promotion mechanism for rule proposals.
///
/// Takes proposals from the consolidation job, validates against policy,
/// optionally requires human approval, commits as active rules, and
/// supports full rollback.
pub struct PromotionGate {
    journal: Arc<dyn Journal>,
    blob_store: Arc<BlobStore>,
    policy: Arc<Mutex<PolicyEngine>>,
    config: PromotionConfig,
    session_id: SessionId,
    branch_id: BranchId,
    /// Local projection rebuilt from events.
    rule_set: Mutex<ActiveRuleSet>,
}

impl PromotionGate {
    pub fn new(
        journal: Arc<dyn Journal>,
        blob_store: Arc<BlobStore>,
        policy: Arc<Mutex<PolicyEngine>>,
        config: PromotionConfig,
        session_id: SessionId,
        branch_id: BranchId,
    ) -> Self {
        Self {
            journal,
            blob_store,
            policy,
            config,
            session_id,
            branch_id,
            rule_set: Mutex::new(ActiveRuleSet::new()),
        }
    }

    /// Rebuild the projection by replaying all learning.* events from the journal.
    pub async fn rebuild_projection(&self) -> Result<(), PromotionError> {
        let query = lago_core::journal::EventQuery::default().session(self.session_id.clone());
        let events = self.journal.read(query).await?;

        let mut rule_set = self
            .rule_set
            .lock()
            .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;
        *rule_set = ActiveRuleSet::new();
        for event in &events {
            rule_set.fold(event);
        }
        Ok(())
    }

    /// Promote a rule proposal to an active rule.
    ///
    /// Checks policy compatibility, enforces limits, and optionally
    /// requires human approval based on confidence threshold.
    pub async fn promote(&self, proposal: &RuleProposal) -> Result<PromotedRule, PromotionError> {
        let now_micros = EventEnvelope::now_micros();

        // Check cooldown
        {
            let rule_set = self
                .rule_set
                .lock()
                .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

            if rule_set.is_in_cooldown(
                &proposal.proposal_id,
                self.config.cooldown_after_revert,
                now_micros,
            ) {
                return Err(PromotionError::CooldownActive(proposal.proposal_id.clone()));
            }

            // Check active rule limit
            if rule_set.active_count() >= self.config.max_active_rules {
                return Err(PromotionError::ActiveRuleLimitReached(
                    self.config.max_active_rules,
                ));
            }
        }

        // Policy conflict detection for AddPolicyRule actions
        if let ProposalAction::AddPolicyRule {
            ref condition_json,
            ref decision,
            ..
        } = proposal.action
        {
            self.check_policy_conflict(condition_json, decision)?;
        }

        // Approval routing
        let needs_approval = self.config.require_human_approval
            && proposal.confidence < self.config.auto_promote_confidence;
        if needs_approval {
            return Err(PromotionError::RequiresApproval);
        }

        // Generate rule ID and store rule content in blob store
        let rule_id = format!("rule-{}", uuid::Uuid::new_v4());
        let rule_content = serde_json::to_vec(proposal)
            .map_err(|e| LagoError::Internal(format!("serialization failed: {e}")))?;
        let _blob_hash = self.blob_store.put(&rule_content)?;

        // Build the promoted rule
        let promoted = PromotedRule {
            rule_id: rule_id.clone(),
            proposal_id: proposal.proposal_id.clone(),
            rule_text: proposal.description.clone(),
            action: proposal.action.clone(),
            promoted_at: 0, // Will be set by journal seq
            reverted: false,
        };

        // Emit learning.promoted event
        let event = build_learning_event(
            &self.session_id,
            &self.branch_id,
            event_types::LEARNING_PROMOTED,
            serde_json::json!({
                "rule_id": rule_id,
                "proposal_id": proposal.proposal_id,
                "rule_text": proposal.description,
                "action": proposal.action,
                "confidence": proposal.confidence,
                "source_learning_ids": proposal.source_learning_ids,
            }),
        );
        let seq = self.journal.append(event).await?;

        // Update local projection
        let mut rule_set = self
            .rule_set
            .lock()
            .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

        let mut promoted_with_seq = promoted;
        promoted_with_seq.promoted_at = seq.into();
        rule_set.rules.push(promoted_with_seq.clone());
        rule_set.history.push(PromotionEvent {
            event_type: event_types::LEARNING_PROMOTED.to_string(),
            rule_id: Some(rule_id),
            proposal_id: proposal.proposal_id.clone(),
            reason: None,
            timestamp: now_micros,
            seq: seq.into(),
        });

        Ok(promoted_with_seq)
    }

    /// Reject a rule proposal with a reason.
    pub async fn reject(&self, proposal_id: &str, reason: &str) -> Result<(), PromotionError> {
        let now_micros = EventEnvelope::now_micros();

        let event = build_learning_event(
            &self.session_id,
            &self.branch_id,
            event_types::LEARNING_REJECTED,
            serde_json::json!({
                "proposal_id": proposal_id,
                "reason": reason,
            }),
        );
        let seq = self.journal.append(event).await?;

        // Update local projection
        let mut rule_set = self
            .rule_set
            .lock()
            .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

        rule_set.history.push(PromotionEvent {
            event_type: event_types::LEARNING_REJECTED.to_string(),
            rule_id: None,
            proposal_id: proposal_id.to_string(),
            reason: Some(reason.to_string()),
            timestamp: now_micros,
            seq: seq.into(),
        });

        Ok(())
    }

    /// Revert a previously promoted rule.
    pub async fn revert(&self, rule_id: &str, reason: &str) -> Result<(), PromotionError> {
        let now_micros = EventEnvelope::now_micros();

        // Find the rule and get its proposal_id
        let proposal_id = {
            let rule_set = self
                .rule_set
                .lock()
                .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

            let rule = rule_set
                .find_rule(rule_id)
                .ok_or_else(|| PromotionError::RuleNotFound(rule_id.to_string()))?;

            if rule.reverted {
                return Err(PromotionError::AlreadyReverted(rule_id.to_string()));
            }

            rule.proposal_id.clone()
        };

        let event = build_learning_event(
            &self.session_id,
            &self.branch_id,
            event_types::LEARNING_REVERTED,
            serde_json::json!({
                "rule_id": rule_id,
                "proposal_id": proposal_id,
                "reason": reason,
            }),
        );
        let seq = self.journal.append(event).await?;

        // Update local projection
        let mut rule_set = self
            .rule_set
            .lock()
            .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

        for rule in &mut rule_set.rules {
            if rule.rule_id == rule_id {
                rule.reverted = true;
                rule_set
                    .reverted_proposals
                    .insert(rule.proposal_id.clone(), now_micros);
            }
        }

        rule_set.history.push(PromotionEvent {
            event_type: event_types::LEARNING_REVERTED.to_string(),
            rule_id: Some(rule_id.to_string()),
            proposal_id,
            reason: Some(reason.to_string()),
            timestamp: now_micros,
            seq: seq.into(),
        });

        Ok(())
    }

    /// Get the current set of active (non-reverted) rules.
    pub fn active_rules(&self) -> Result<Vec<PromotedRule>, PromotionError> {
        let rule_set = self
            .rule_set
            .lock()
            .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

        Ok(rule_set.active_rules().into_iter().cloned().collect())
    }

    /// Get the full promotion history (audit trail).
    pub fn promotion_history(&self) -> Result<Vec<PromotionEvent>, PromotionError> {
        let rule_set = self
            .rule_set
            .lock()
            .map_err(|e| LagoError::Internal(format!("lock poisoned: {e}")))?;

        Ok(rule_set.history().to_vec())
    }

    /// Check whether a proposed policy rule conflicts with existing rules.
    fn check_policy_conflict(
        &self,
        condition_json: &serde_json::Value,
        decision: &str,
    ) -> Result<(), PromotionError> {
        let policy = self
            .policy
            .lock()
            .map_err(|e| LagoError::Internal(format!("policy lock poisoned: {e}")))?;

        // Parse the proposed condition to check for direct contradictions
        let proposed_condition: Result<lago_policy::MatchCondition, _> =
            serde_json::from_value(condition_json.clone());

        let Ok(proposed) = proposed_condition else {
            return Err(PromotionError::PolicyConflict(
                "invalid condition format".to_string(),
            ));
        };

        // Check for contradictions: same condition with opposite decision
        let proposed_decision = match decision {
            "allow" | "Allow" => PolicyDecisionKind::Allow,
            "deny" | "Deny" => PolicyDecisionKind::Deny,
            "require_approval" | "RequireApproval" => PolicyDecisionKind::RequireApproval,
            other => {
                return Err(PromotionError::PolicyConflict(format!(
                    "unknown decision kind: {other}"
                )));
            }
        };

        // Simple conflict detection: if any existing rule has the same
        // serialized condition but opposite decision, flag it.
        for existing in policy.rules() {
            let existing_json = serde_json::to_value(&existing.condition).unwrap_or_default();
            let proposed_json = serde_json::to_value(&proposed).unwrap_or_default();

            if existing_json == proposed_json && existing.decision != proposed_decision {
                return Err(PromotionError::PolicyConflict(format!(
                    "contradicts existing rule '{}' ({}): same condition, different decision",
                    existing.name, existing.id
                )));
            }
        }

        Ok(())
    }
}

// ── Helpers ─────────────────────────────────────────────────────────

/// Build a Lago `EventEnvelope` for a learning event.
fn build_learning_event(
    session_id: &SessionId,
    branch_id: &BranchId,
    event_type: &str,
    data: serde_json::Value,
) -> EventEnvelope {
    EventEnvelope {
        event_id: EventId::new(),
        session_id: session_id.clone(),
        branch_id: branch_id.clone(),
        run_id: None,
        seq: 0, // Auto-assigned by journal
        timestamp: EventEnvelope::now_micros(),
        parent_id: None,
        payload: EventPayload::Custom {
            event_type: event_type.to_string(),
            data,
        },
        metadata: HashMap::new(),
        schema_version: 1,
    }
}

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

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

    fn make_custom_event(
        event_type: &str,
        data: serde_json::Value,
        seq: u64,
        timestamp: u64,
    ) -> EventEnvelope {
        EventEnvelope {
            event_id: EventId::new(),
            session_id: SessionId::from_string("test"),
            branch_id: BranchId::from_string("main"),
            run_id: None,
            seq,
            timestamp,
            parent_id: None,
            payload: EventPayload::Custom {
                event_type: event_type.to_string(),
                data,
            },
            metadata: HashMap::new(),
            schema_version: 1,
        }
    }

    fn sample_proposal(id: &str, confidence: f64) -> RuleProposal {
        RuleProposal {
            proposal_id: id.to_string(),
            description: format!("Test rule from proposal {id}"),
            action: ProposalAction::AddSystemPromptGuidance(
                "Always check file permissions before writing".to_string(),
            ),
            confidence,
            source_learning_ids: vec!["learn-1".to_string()],
            session_id: "sess-1".to_string(),
        }
    }

    fn sample_policy_proposal(id: &str, confidence: f64) -> RuleProposal {
        RuleProposal {
            proposal_id: id.to_string(),
            description: "Deny shell access".to_string(),
            action: ProposalAction::AddPolicyRule {
                rule_id: format!("pr-{id}"),
                name: "deny shell".to_string(),
                priority: 10,
                condition_json: serde_json::json!({"type": "ToolName", "value": "exec_shell"}),
                decision: "Deny".to_string(),
                explanation: Some("shell access denied by learning".to_string()),
            },
            confidence,
            source_learning_ids: vec!["learn-2".to_string()],
            session_id: "sess-1".to_string(),
        }
    }

    // ── Projection tests ────────────────────────────────────────────

    #[test]
    fn projection_promotes_rule() {
        let mut proj = ActiveRuleSet::new();

        proj.fold(&make_custom_event(
            event_types::LEARNING_PROMOTED,
            serde_json::json!({
                "rule_id": "r1",
                "proposal_id": "p1",
                "rule_text": "Always verify permissions",
                "action": {"type": "AddSystemPromptGuidance", "value": "Always verify permissions"},
            }),
            1,
            1000,
        ));

        assert_eq!(proj.active_count(), 1);
        let active = proj.active_rules();
        assert_eq!(active[0].rule_id, "r1");
        assert_eq!(active[0].proposal_id, "p1");
        assert!(!active[0].reverted);
    }

    #[test]
    fn projection_rejects_proposal() {
        let mut proj = ActiveRuleSet::new();

        proj.fold(&make_custom_event(
            event_types::LEARNING_REJECTED,
            serde_json::json!({
                "proposal_id": "p2",
                "reason": "too risky",
            }),
            1,
            1000,
        ));

        // Rejected proposals don't appear in active rules
        assert_eq!(proj.active_count(), 0);
        // But they show up in history
        assert_eq!(proj.history().len(), 1);
        assert_eq!(proj.history()[0].event_type, event_types::LEARNING_REJECTED);
        assert_eq!(proj.history()[0].reason.as_deref(), Some("too risky"));
    }

    #[test]
    fn projection_reverts_promoted_rule() {
        let mut proj = ActiveRuleSet::new();

        // Promote
        proj.fold(&make_custom_event(
            event_types::LEARNING_PROMOTED,
            serde_json::json!({
                "rule_id": "r1",
                "proposal_id": "p1",
                "rule_text": "Check perms",
                "action": {"type": "AddSystemPromptGuidance", "value": "Check perms"},
            }),
            1,
            1000,
        ));
        assert_eq!(proj.active_count(), 1);

        // Revert
        proj.fold(&make_custom_event(
            event_types::LEARNING_REVERTED,
            serde_json::json!({
                "rule_id": "r1",
                "proposal_id": "p1",
                "reason": "caused regressions",
            }),
            2,
            2000,
        ));

        assert_eq!(proj.active_count(), 0);
        assert_eq!(proj.history().len(), 2);
    }

    #[test]
    fn projection_cooldown_after_revert() {
        let mut proj = ActiveRuleSet::new();

        // Promote and revert
        proj.fold(&make_custom_event(
            event_types::LEARNING_PROMOTED,
            serde_json::json!({
                "rule_id": "r1",
                "proposal_id": "p1",
                "rule_text": "Check perms",
            }),
            1,
            1_000_000,
        ));
        proj.fold(&make_custom_event(
            event_types::LEARNING_REVERTED,
            serde_json::json!({
                "rule_id": "r1",
                "proposal_id": "p1",
                "reason": "bad rule",
            }),
            2,
            2_000_000,
        ));

        // Immediately after revert: still in cooldown
        let cooldown = Duration::from_secs(3600); // 1 hour
        assert!(proj.is_in_cooldown("p1", cooldown, 2_500_000));

        // After cooldown expires: no longer in cooldown
        let far_future = 2_000_000 + 3_600_000_001; // revert_time + 1h + 1μs
        assert!(!proj.is_in_cooldown("p1", cooldown, far_future));

        // Unrelated proposal: never in cooldown
        assert!(!proj.is_in_cooldown("p999", cooldown, 2_500_000));
    }

    #[test]
    fn projection_ignores_unrelated_events() {
        let mut proj = ActiveRuleSet::new();

        proj.fold(&make_custom_event(
            "skill.activated",
            serde_json::json!({"name": "test"}),
            1,
            1000,
        ));

        assert_eq!(proj.active_count(), 0);
        assert!(proj.history().is_empty());
    }

    #[test]
    fn projection_multiple_rules_active() {
        let mut proj = ActiveRuleSet::new();

        for i in 0..5 {
            proj.fold(&make_custom_event(
                event_types::LEARNING_PROMOTED,
                serde_json::json!({
                    "rule_id": format!("r{i}"),
                    "proposal_id": format!("p{i}"),
                    "rule_text": format!("Rule {i}"),
                }),
                i as u64,
                (i * 1000) as u64,
            ));
        }

        assert_eq!(proj.active_count(), 5);

        // Revert middle rule
        proj.fold(&make_custom_event(
            event_types::LEARNING_REVERTED,
            serde_json::json!({
                "rule_id": "r2",
                "proposal_id": "p2",
                "reason": "not useful",
            }),
            5,
            5000,
        ));

        assert_eq!(proj.active_count(), 4);
        assert!(proj.find_rule("r2").unwrap().reverted);
        assert!(!proj.find_rule("r3").unwrap().reverted);
    }

    #[test]
    fn projection_name() {
        let proj = ActiveRuleSet::new();
        assert_eq!(proj.name(), "arcan::active_rules");
    }

    // ── Serialization tests ─────────────────────────────────────────

    #[test]
    fn proposal_action_serialization_roundtrip() {
        let actions = vec![
            ProposalAction::AddSystemPromptGuidance("Check perms".to_string()),
            ProposalAction::AddPolicyRule {
                rule_id: "r1".to_string(),
                name: "deny shell".to_string(),
                priority: 10,
                condition_json: serde_json::json!({"type": "ToolName", "value": "exec_shell"}),
                decision: "Deny".to_string(),
                explanation: Some("no shell".to_string()),
            },
            ProposalAction::ModifyToolConfig {
                tool_name: "bash".to_string(),
                config_patch: serde_json::json!({"timeout_ms": 5000}),
            },
        ];

        for action in &actions {
            let json = serde_json::to_string(action).unwrap();
            let back: ProposalAction = serde_json::from_str(&json).unwrap();
            assert_eq!(&back, action);
        }
    }

    #[test]
    fn rule_proposal_serialization() {
        let proposal = sample_proposal("p1", 0.85);
        let json = serde_json::to_string(&proposal).unwrap();
        let back: RuleProposal = serde_json::from_str(&json).unwrap();
        assert_eq!(back.proposal_id, "p1");
        assert_eq!(back.confidence, 0.85);
    }

    // ── Gate integration tests (async, with real journal) ───────────

    async fn setup_gate(config: PromotionConfig) -> PromotionGate {
        let dir = tempfile::tempdir().unwrap();
        let journal =
            Arc::new(lago_journal::RedbJournal::open(dir.path().join("test.redb")).unwrap());
        let blob_store = Arc::new(BlobStore::from_path(dir.path().join("blobs")).unwrap());
        let policy = Arc::new(Mutex::new(PolicyEngine::new()));

        // Create session
        let session = lago_core::Session {
            session_id: SessionId::from_string("sess-1"),
            config: lago_core::session::SessionConfig::new("test"),
            created_at: 0,
            branches: vec![],
        };
        journal.put_session(session).await.unwrap();

        PromotionGate::new(
            journal,
            blob_store,
            policy,
            config,
            SessionId::from_string("sess-1"),
            BranchId::from_string("main"),
        )
    }

    #[tokio::test]
    async fn gate_promote_creates_active_rule() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: false,
            auto_promote_confidence: 0.5,
            ..Default::default()
        })
        .await;

        let proposal = sample_proposal("p1", 0.9);
        let promoted = gate.promote(&proposal).await.unwrap();

        assert_eq!(promoted.proposal_id, "p1");
        assert!(!promoted.reverted);

        let active = gate.active_rules().unwrap();
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].proposal_id, "p1");
    }

    #[tokio::test]
    async fn gate_reject_emits_event() {
        let gate = setup_gate(PromotionConfig::default()).await;

        gate.reject("p1", "not useful").await.unwrap();

        let history = gate.promotion_history().unwrap();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].event_type, event_types::LEARNING_REJECTED);
        assert_eq!(history[0].reason.as_deref(), Some("not useful"));

        // No active rules
        assert!(gate.active_rules().unwrap().is_empty());
    }

    #[tokio::test]
    async fn gate_revert_removes_from_active() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: false,
            auto_promote_confidence: 0.5,
            ..Default::default()
        })
        .await;

        let proposal = sample_proposal("p1", 0.9);
        let promoted = gate.promote(&proposal).await.unwrap();

        // Revert it
        gate.revert(&promoted.rule_id, "caused regressions")
            .await
            .unwrap();

        assert!(gate.active_rules().unwrap().is_empty());

        let history = gate.promotion_history().unwrap();
        assert_eq!(history.len(), 2);
        assert_eq!(history[1].event_type, event_types::LEARNING_REVERTED);
    }

    #[tokio::test]
    async fn gate_auto_promote_above_threshold() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: true,
            auto_promote_confidence: 0.90,
            ..Default::default()
        })
        .await;

        // High confidence → auto-promote even with require_human_approval
        let proposal = sample_proposal("p1", 0.95);
        let result = gate.promote(&proposal).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn gate_requires_approval_below_threshold() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: true,
            auto_promote_confidence: 0.95,
            ..Default::default()
        })
        .await;

        // Low confidence + require_human_approval → RequiresApproval error
        let proposal = sample_proposal("p1", 0.80);
        let result = gate.promote(&proposal).await;
        assert!(matches!(result, Err(PromotionError::RequiresApproval)));
    }

    #[tokio::test]
    async fn gate_enforces_max_active_rules() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: false,
            auto_promote_confidence: 0.5,
            max_active_rules: 3,
            ..Default::default()
        })
        .await;

        // Fill up to the limit
        for i in 0..3 {
            let proposal = sample_proposal(&format!("p{i}"), 0.9);
            gate.promote(&proposal).await.unwrap();
        }

        // Fourth should fail
        let proposal = sample_proposal("p3", 0.9);
        let result = gate.promote(&proposal).await;
        assert!(matches!(
            result,
            Err(PromotionError::ActiveRuleLimitReached(3))
        ));
    }

    #[tokio::test]
    async fn gate_policy_conflict_detection() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: false,
            auto_promote_confidence: 0.5,
            ..Default::default()
        })
        .await;

        // Add an existing rule to the policy engine
        {
            let mut policy = gate.policy.lock().unwrap();
            policy.add_rule(lago_policy::Rule {
                id: "existing-1".to_string(),
                name: "allow shell".to_string(),
                priority: 10,
                condition: lago_policy::MatchCondition::ToolName("exec_shell".to_string()),
                decision: PolicyDecisionKind::Allow,
                explanation: None,
                required_sandbox: None,
            });
        }

        // Propose a contradicting rule (same condition, Deny vs Allow)
        let proposal = sample_policy_proposal("conflict-1", 0.99);
        let result = gate.promote(&proposal).await;
        assert!(matches!(result, Err(PromotionError::PolicyConflict(_))));
    }

    #[tokio::test]
    async fn gate_revert_nonexistent_rule_fails() {
        let gate = setup_gate(PromotionConfig::default()).await;

        let result = gate.revert("nonexistent", "test").await;
        assert!(matches!(result, Err(PromotionError::RuleNotFound(_))));
    }

    #[tokio::test]
    async fn gate_double_revert_fails() {
        let gate = setup_gate(PromotionConfig {
            require_human_approval: false,
            auto_promote_confidence: 0.5,
            ..Default::default()
        })
        .await;

        let proposal = sample_proposal("p1", 0.9);
        let promoted = gate.promote(&proposal).await.unwrap();

        gate.revert(&promoted.rule_id, "first revert")
            .await
            .unwrap();

        let result = gate.revert(&promoted.rule_id, "second revert").await;
        assert!(matches!(result, Err(PromotionError::AlreadyReverted(_))));
    }
}