forge-pilot 0.1.0

Closed-loop orchestrator over runtime advisories, kernel oracles, and canonical Forge export/import lanes
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
//! Governance surface checks for the forge-pilot OODA loop.
//!
//! This module evaluates governance artifact state during observation
//! and gates execution during the act phase. It does not own authority —
//! it reads governance artifacts and reports their status to the loop.
//!
//! ## Observation scope
//!
//! The following six predicates are checked by `observe_governance()`:
//!
//! 1. **Effect preflight status** — reads the latest effect-runtime preflight disposition.
//! 2. **Assurance readiness** — checks whether an assurance-runtime case is release-ready.
//! 3. **Authority delegation validity** — verifies the authority-delegation chain is intact.
//! 4. **Continuity incident state** — detects active continuity-runtime incidents.
//! 5. **Constitutional amendment state** — detects pending constitutional-memory amendments.
//! 6. **Mechanism fit disposition** — reads the latest mechanism-runtime fit evaluation.
//!
//! ## Not yet observed
//!
//! - **Attestation exchange state** — `attestation-exchange` is wired but not yet consumed
//!   by the observation pipeline. See GOV-002 / SCOPE_NOTES.md.
//! - **Detailed mechanism state** — only the fit disposition is observed, not internal
//!   mechanism-runtime evaluation details.
//! - **Detailed assurance state** — only the ready/not-ready flag is observed, not the
//!   full assurance case tree.
//!
//! ## Why the current scope is sufficient for CLARA V1
//!
//! The six observed predicates cover all governance surfaces that can block or degrade
//! execution in the OODA loop. The unobserved surfaces (attestation, detailed mechanism
//! and assurance state) are informational and do not gate any execution decision in
//! the current pipeline. They are planned for V2.
//!
//! ## Design constraints
//!
//! - **Read-only observation.** `observe_governance()` reads governance artifact state.
//!   It never writes, creates, or modifies governance artifacts.
//! - **Fail-open on missing governance state.** When no governance artifacts are
//!   present, the gate returns an empty observation and the loop proceeds normally.
//! - **No external dependencies.** Reads only from semantic-memory's SQLite store.

use schemars::JsonSchema;
use semantic_memory::{MemoryStore, ProjectionClaimVersion, ProjectionQuery};
use serde::{Deserialize, Serialize};
use stack_ids::ScopeKey;

/// LIB-001: Governance enforcement mode.
///
/// `FailOpen` preserves existing behavior — missing or broken governance
/// artifacts produce a default observation and execution proceeds.
/// `Strict` fails closed — observation errors and missing governance
/// artifacts produce a `GovernanceGateError` and the caller must handle it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum GovernanceMode {
    /// Existing default: return empty observation on errors (fail-open).
    #[default]
    FailOpen,
    /// Constitutional mode: return error on missing/broken governance (fail-closed).
    Strict,
}

/// LIB-001: Error returned when strict governance mode detects a problem.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
pub enum GovernanceGateError {
    /// Governance observation failed and strict mode does not allow fallback.
    #[error("governance observation failed in strict mode: {reason}")]
    ObservationFailed { reason: String },
    /// No governance claims found — strict mode requires governance artifacts.
    #[error("no governance claims found in strict mode")]
    NoGovernanceClaims,
}

/// Observed governance artifact state at a point in time.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct GovernanceObservation {
    /// Effect preflight disposition, if any governance effect artifact was found.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effect_preflight_status: Option<String>,
    /// Whether an assurance case is ready for release.
    #[serde(default)]
    pub assurance_ready: bool,
    /// Whether authority delegation chain is valid.
    #[serde(default)]
    pub authority_delegation_valid: bool,
    /// Whether a continuity incident is currently active.
    #[serde(default)]
    pub continuity_incident_active: bool,
    /// Whether a constitutional amendment is pending.
    #[serde(default)]
    pub constitutional_amendment_pending: bool,
    /// Mechanism fit disposition, if evaluated.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mechanism_fit_disposition: Option<String>,
    /// Any governance degradations detected.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub governance_degradations: Vec<GovernanceDegradation>,
}

/// A degradation in a governance surface.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct GovernanceDegradation {
    pub surface: String,
    pub reason: String,
    pub blocks_promotion: bool,
}

/// Result of the governance gate evaluation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum GovernanceGateResult {
    /// Governance state allows execution to proceed.
    Allow,
    /// Governance state recommends advisory-only execution (no promotion).
    AdvisoryOnly { reason: String },
    /// Governance state blocks execution.
    Blocked { reason: String },
}

/// Typed receipt for governance observation within a loop iteration.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct GovernanceReceiptV1 {
    pub schema_version: String,
    pub gate_result: GovernanceGateResult,
    pub observation_summary: GovernanceObservationSummary,
}

/// Summary of governance observation included in the receipt.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct GovernanceObservationSummary {
    pub effect_preflight_present: bool,
    pub assurance_ready: bool,
    pub authority_delegation_valid: bool,
    pub continuity_incident_active: bool,
    pub constitutional_amendment_pending: bool,
    pub mechanism_fit_present: bool,
    pub degradation_count: usize,
}

pub const GOVERNANCE_RECEIPT_V1_SCHEMA: &str = "governance_receipt_v1";

/// Governance projection family constant. Claims projected with this family are
/// governance artifacts readable by `observe_governance()`.
pub const GOVERNANCE_PROJECTION_FAMILY: &str = "governance";

/// Governance-scoped namespace in semantic-memory claim projections.
pub const GOVERNANCE_SCOPE_NAMESPACE: &str = "governance";

/// Well-known predicate constants for governance claim projections.
pub mod predicates {
    /// Predicate for effect preflight disposition claims.
    pub const EFFECT_PREFLIGHT: &str = "effect_preflight_status";
    /// Predicate for assurance case readiness claims.
    pub const ASSURANCE_READY: &str = "assurance_ready";
    /// Predicate for authority delegation chain validity claims.
    pub const AUTHORITY_DELEGATION_VALID: &str = "authority_chain_validity";
    /// Predicate for continuity incident active status claims.
    pub const CONTINUITY_INCIDENT_ACTIVE: &str = "continuity_incident_active";
    /// Predicate for constitutional amendment pending status claims.
    pub const CONSTITUTIONAL_AMENDMENT_PENDING: &str = "constitutional_amendment_pending";
    /// Predicate for mechanism fit disposition claims.
    pub const MECHANISM_FIT: &str = "mechanism_fit_disposition";
}

/// Observes governance artifact state from semantic-memory projections.
///
/// Queries the `claim_versions` table in the governance scope namespace for
/// claim projections that represent governance artifact state. Each governance
/// surface is identified by its predicate value:
///
/// - `effect_preflight_status` — effect-runtime preflight disposition
/// - `assurance_ready` — assurance-runtime case readiness
/// - `authority_chain_validity` — authority-delegation chain state
/// - `continuity_incident_active` — continuity-runtime incident state
/// - `constitutional_amendment_pending` — constitutional-memory amendment state
/// - `mechanism_fit_disposition` — mechanism-runtime fit run disposition
///
/// Returns a default (empty) observation on any error — fail-open by design.
/// This function is read-only and never writes governance artifacts.
///
/// For strict (fail-closed) behavior, use [`observe_governance_with_mode`]
/// with [`GovernanceMode::Strict`].
pub async fn observe_governance(store: &MemoryStore) -> GovernanceObservation {
    match observe_governance_inner(store).await {
        Ok(obs) => obs,
        Err(err) => {
            tracing::warn!(
                error = %err,
                "governance observation failed, returning default (fail-open)"
            );
            GovernanceObservation::default()
        }
    }
}

/// LIB-001: Observe governance with explicit mode selection.
///
/// In `FailOpen` mode, behaves identically to [`observe_governance`].
/// In `Strict` mode, returns `Err` when governance observation fails or
/// when no governance claims are found. This forces callers to explicitly
/// handle the absence of governance artifacts rather than silently proceeding.
pub async fn observe_governance_with_mode(
    store: &MemoryStore,
    mode: GovernanceMode,
) -> Result<GovernanceObservation, GovernanceGateError> {
    match observe_governance_inner(store).await {
        Ok(obs) => {
            // LIB-001: In strict mode, require at least one governance claim.
            if mode == GovernanceMode::Strict && is_empty_observation(&obs) {
                tracing::warn!(
                    "strict governance mode: no governance claims found, failing closed"
                );
                return Err(GovernanceGateError::NoGovernanceClaims);
            }
            Ok(obs)
        }
        Err(err) => match mode {
            GovernanceMode::FailOpen => {
                tracing::warn!(
                    error = %err,
                    "governance observation failed, returning default (fail-open)"
                );
                Ok(GovernanceObservation::default())
            }
            GovernanceMode::Strict => {
                tracing::error!(
                    error = %err,
                    "governance observation failed in strict mode, failing closed"
                );
                Err(GovernanceGateError::ObservationFailed {
                    reason: err.to_string(),
                })
            }
        },
    }
}

/// Returns true if the observation has no governance data populated.
fn is_empty_observation(obs: &GovernanceObservation) -> bool {
    obs.effect_preflight_status.is_none()
        && !obs.assurance_ready
        && !obs.authority_delegation_valid
        && !obs.continuity_incident_active
        && !obs.constitutional_amendment_pending
        && obs.mechanism_fit_disposition.is_none()
        && obs.governance_degradations.is_empty()
}

/// Inner implementation that can propagate errors. The outer function catches
/// all errors and returns Default (fail-open).
async fn observe_governance_inner(
    store: &MemoryStore,
) -> Result<GovernanceObservation, semantic_memory::MemoryError> {
    let query = ProjectionQuery {
        scope: ScopeKey {
            namespace: GOVERNANCE_SCOPE_NAMESPACE.to_string(),
            domain: None,
            workspace_id: None,
            repo_id: None,
        },
        text_query: None,
        valid_at: None,
        recorded_at_or_before: None,
        subject_entity_id: None,
        canonical_entity_id: None,
        claim_state: Some("active".to_string()),
        claim_id: None,
        claim_version_id: None,
        limit: 100,
    };

    let claims = store.query_claim_versions(query).await?;
    if claims.is_empty() {
        tracing::debug!("no governance claims found in scope, returning default observation");
        return Ok(GovernanceObservation::default());
    }

    // Filter to governance projection family claims.
    let gov_claims: Vec<&ProjectionClaimVersion> = claims
        .iter()
        .filter(|c| c.projection_family == GOVERNANCE_PROJECTION_FAMILY)
        .collect();

    if gov_claims.is_empty() {
        tracing::debug!(
            total_claims = claims.len(),
            "claims found but none in governance projection family"
        );
        return Ok(GovernanceObservation::default());
    }

    let mut observation = GovernanceObservation::default();
    let mut degradations = Vec::new();

    for claim in &gov_claims {
        match claim.predicate.as_str() {
            predicates::EFFECT_PREFLIGHT => {
                observation.effect_preflight_status = Some(claim.content.clone());
            }
            predicates::ASSURANCE_READY => {
                observation.assurance_ready = parse_bool_claim(&claim.content);
            }
            predicates::AUTHORITY_DELEGATION_VALID => {
                observation.authority_delegation_valid = parse_bool_claim(&claim.content);
            }
            predicates::CONTINUITY_INCIDENT_ACTIVE => {
                observation.continuity_incident_active = parse_bool_claim(&claim.content);
            }
            predicates::CONSTITUTIONAL_AMENDMENT_PENDING => {
                observation.constitutional_amendment_pending = parse_bool_claim(&claim.content);
            }
            predicates::MECHANISM_FIT => {
                observation.mechanism_fit_disposition = Some(claim.content.clone());
            }
            other => {
                tracing::trace!(
                    predicate = other,
                    "unrecognized governance predicate, skipping"
                );
            }
        }

        // If the claim is stale or contradicted, record a degradation.
        if claim.freshness != "current" || claim.contradiction_status != "none" {
            degradations.push(GovernanceDegradation {
                surface: claim.predicate.clone(),
                reason: format!(
                    "freshness={}, contradiction={}",
                    claim.freshness, claim.contradiction_status
                ),
                blocks_promotion: claim.freshness == "superseded"
                    || claim.contradiction_status != "none",
            });
        }
    }

    observation.governance_degradations = degradations;

    tracing::debug!(
        claim_count = gov_claims.len(),
        effect_preflight = observation.effect_preflight_status.is_some(),
        assurance_ready = observation.assurance_ready,
        authority_valid = observation.authority_delegation_valid,
        incident_active = observation.continuity_incident_active,
        amendment_pending = observation.constitutional_amendment_pending,
        mechanism_fit = observation.mechanism_fit_disposition.is_some(),
        degradation_count = observation.governance_degradations.len(),
        "governance observation populated from semantic-memory projections"
    );

    Ok(observation)
}

/// Parse a claim content string as a boolean. Supports "true"/"false"
/// and "1"/"0". Returns false for unrecognized values (fail-safe).
fn parse_bool_claim(content: &str) -> bool {
    matches!(content.trim().to_lowercase().as_str(), "true" | "1" | "yes")
}

/// Evaluates whether governance state permits execution to proceed.
///
/// For strict-mode gating that returns an error on governance failures,
/// use [`gate_execution_with_mode`].
pub fn gate_execution(observation: &GovernanceObservation) -> GovernanceGateResult {
    // Active continuity incident blocks execution.
    if observation.continuity_incident_active {
        return GovernanceGateResult::Blocked {
            reason: "continuity incident is active".into(),
        };
    }
    // Invalid authority delegation blocks execution.
    if observation.effect_preflight_status.is_some() && !observation.authority_delegation_valid {
        return GovernanceGateResult::Blocked {
            reason: "authority delegation chain is not valid".into(),
        };
    }
    // Pending constitutional amendment downgrades to advisory-only.
    if observation.constitutional_amendment_pending {
        return GovernanceGateResult::AdvisoryOnly {
            reason: "constitutional amendment is pending".into(),
        };
    }
    // Promotion-blocking degradations downgrade to advisory-only.
    if observation
        .governance_degradations
        .iter()
        .any(|d| d.blocks_promotion)
    {
        return GovernanceGateResult::AdvisoryOnly {
            reason: "governance degradation blocks promotion".into(),
        };
    }
    GovernanceGateResult::Allow
}

/// LIB-001: Evaluates governance with explicit mode.
///
/// In `Strict` mode, a `Blocked` result is promoted to an error so callers
/// cannot accidentally ignore it. `Allow` and `AdvisoryOnly` pass through.
/// In `FailOpen` mode, behaves identically to [`gate_execution`].
pub fn gate_execution_with_mode(
    observation: &GovernanceObservation,
    mode: GovernanceMode,
) -> Result<GovernanceGateResult, GovernanceGateError> {
    let result = gate_execution(observation);
    match (&result, mode) {
        (GovernanceGateResult::Blocked { reason }, GovernanceMode::Strict) => {
            Err(GovernanceGateError::ObservationFailed {
                reason: format!("governance blocked in strict mode: {reason}"),
            })
        }
        _ => Ok(result),
    }
}

/// Builds a typed governance receipt for a loop iteration report.
pub fn build_governance_receipt(
    observation: &GovernanceObservation,
    gate_result: &GovernanceGateResult,
) -> GovernanceReceiptV1 {
    GovernanceReceiptV1 {
        schema_version: GOVERNANCE_RECEIPT_V1_SCHEMA.into(),
        gate_result: gate_result.clone(),
        observation_summary: GovernanceObservationSummary {
            effect_preflight_present: observation.effect_preflight_status.is_some(),
            assurance_ready: observation.assurance_ready,
            authority_delegation_valid: observation.authority_delegation_valid,
            continuity_incident_active: observation.continuity_incident_active,
            constitutional_amendment_pending: observation.constitutional_amendment_pending,
            mechanism_fit_present: observation.mechanism_fit_disposition.is_some(),
            degradation_count: observation.governance_degradations.len(),
        },
    }
}

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

    #[test]
    fn default_observation_allows_execution() {
        let obs = GovernanceObservation::default();
        let result = gate_execution(&obs);
        assert_eq!(result, GovernanceGateResult::Allow);
    }

    #[test]
    fn active_incident_blocks_execution() {
        let obs = GovernanceObservation {
            continuity_incident_active: true,
            ..Default::default()
        };
        let result = gate_execution(&obs);
        assert!(matches!(result, GovernanceGateResult::Blocked { .. }));
    }

    #[test]
    fn pending_amendment_downgrades_to_advisory() {
        let obs = GovernanceObservation {
            constitutional_amendment_pending: true,
            ..Default::default()
        };
        let result = gate_execution(&obs);
        assert!(matches!(result, GovernanceGateResult::AdvisoryOnly { .. }));
    }

    #[test]
    fn governance_receipt_roundtrip() {
        let obs = GovernanceObservation::default();
        let gate = gate_execution(&obs);
        let receipt = build_governance_receipt(&obs, &gate);
        assert_eq!(receipt.schema_version, GOVERNANCE_RECEIPT_V1_SCHEMA);
        let json = serde_json::to_string(&receipt).unwrap();
        let roundtrip: GovernanceReceiptV1 = serde_json::from_str(&json).unwrap();
        assert_eq!(roundtrip.schema_version, GOVERNANCE_RECEIPT_V1_SCHEMA);
    }

    #[test]
    fn parse_bool_claim_values() {
        assert!(parse_bool_claim("true"));
        assert!(parse_bool_claim("True"));
        assert!(parse_bool_claim("TRUE"));
        assert!(parse_bool_claim("1"));
        assert!(parse_bool_claim("yes"));
        assert!(parse_bool_claim("  true  "));
        assert!(!parse_bool_claim("false"));
        assert!(!parse_bool_claim("0"));
        assert!(!parse_bool_claim("no"));
        assert!(!parse_bool_claim(""));
        assert!(!parse_bool_claim("unknown"));
    }

    #[test]
    fn degradation_blocks_promotion_downgrades_to_advisory() {
        let obs = GovernanceObservation {
            governance_degradations: vec![GovernanceDegradation {
                surface: "effect_preflight_status".into(),
                reason: "freshness=superseded, contradiction=none".into(),
                blocks_promotion: true,
            }],
            ..Default::default()
        };
        let result = gate_execution(&obs);
        assert!(matches!(result, GovernanceGateResult::AdvisoryOnly { .. }));
    }

    #[test]
    fn non_blocking_degradation_allows_execution() {
        let obs = GovernanceObservation {
            governance_degradations: vec![GovernanceDegradation {
                surface: "mechanism_fit_disposition".into(),
                reason: "freshness=stale, contradiction=none".into(),
                blocks_promotion: false,
            }],
            ..Default::default()
        };
        let result = gate_execution(&obs);
        assert_eq!(result, GovernanceGateResult::Allow);
    }

    #[test]
    fn invalid_authority_without_preflight_allows() {
        // authority_delegation_valid is false but no effect_preflight_status is present,
        // so execution is not blocked
        let obs = GovernanceObservation {
            authority_delegation_valid: false,
            ..Default::default()
        };
        let result = gate_execution(&obs);
        assert_eq!(result, GovernanceGateResult::Allow);
    }

    #[test]
    fn invalid_authority_with_preflight_blocks() {
        let obs = GovernanceObservation {
            effect_preflight_status: Some("commit_eligible".into()),
            authority_delegation_valid: false,
            ..Default::default()
        };
        let result = gate_execution(&obs);
        assert!(matches!(result, GovernanceGateResult::Blocked { .. }));
    }

    /// Verifies that observe_governance returns default when the store has no
    /// governance claims (empty store, fail-open).
    #[tokio::test]
    async fn observe_governance_empty_store_returns_default() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let config = semantic_memory::MemoryConfig {
            base_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let store = MemoryStore::open(config).expect("open store");
        let obs = observe_governance(&store).await;
        assert_eq!(obs.effect_preflight_status, None);
        assert!(!obs.assurance_ready);
        assert!(!obs.authority_delegation_valid);
        assert!(!obs.continuity_incident_active);
        assert!(!obs.constitutional_amendment_pending);
        assert_eq!(obs.mechanism_fit_disposition, None);
        assert!(obs.governance_degradations.is_empty());
        // Default observation allows execution.
        assert_eq!(gate_execution(&obs), GovernanceGateResult::Allow);
    }

    /// Verifies that observe_governance populates observation fields from
    /// governance claim projections stored in semantic-memory.
    #[tokio::test]
    async fn observe_governance_reads_real_artifacts() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let config = semantic_memory::MemoryConfig {
            base_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let store = MemoryStore::open(config).expect("open store");

        // Insert governance claims directly into the claim_versions table.
        insert_governance_claim(&store, predicates::EFFECT_PREFLIGHT, "commit_eligible").await;
        insert_governance_claim(&store, predicates::ASSURANCE_READY, "true").await;
        insert_governance_claim(&store, predicates::AUTHORITY_DELEGATION_VALID, "true").await;
        insert_governance_claim(&store, predicates::CONTINUITY_INCIDENT_ACTIVE, "false").await;
        insert_governance_claim(&store, predicates::CONSTITUTIONAL_AMENDMENT_PENDING, "true").await;
        insert_governance_claim(
            &store,
            predicates::MECHANISM_FIT,
            "eligible_for_local_review",
        )
        .await;

        let obs = observe_governance(&store).await;

        // Verify non-default observation was populated.
        assert_eq!(
            obs.effect_preflight_status.as_deref(),
            Some("commit_eligible")
        );
        assert!(obs.assurance_ready);
        assert!(obs.authority_delegation_valid);
        assert!(!obs.continuity_incident_active);
        assert!(obs.constitutional_amendment_pending);
        assert_eq!(
            obs.mechanism_fit_disposition.as_deref(),
            Some("eligible_for_local_review")
        );

        // Constitutional amendment pending => advisory-only.
        let gate = gate_execution(&obs);
        assert!(
            matches!(gate, GovernanceGateResult::AdvisoryOnly { .. }),
            "expected AdvisoryOnly due to pending amendment, got: {:?}",
            gate
        );

        // Receipt should capture the observation accurately.
        let receipt = build_governance_receipt(&obs, &gate);
        assert!(receipt.observation_summary.effect_preflight_present);
        assert!(receipt.observation_summary.assurance_ready);
        assert!(receipt.observation_summary.authority_delegation_valid);
        assert!(!receipt.observation_summary.continuity_incident_active);
        assert!(receipt.observation_summary.constitutional_amendment_pending);
        assert!(receipt.observation_summary.mechanism_fit_present);
    }

    /// Verifies that an active incident claim causes observe_governance
    /// to report it and gate_execution to block.
    #[tokio::test]
    async fn observe_governance_active_incident_blocks() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let config = semantic_memory::MemoryConfig {
            base_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let store = MemoryStore::open(config).expect("open store");

        insert_governance_claim(&store, predicates::CONTINUITY_INCIDENT_ACTIVE, "true").await;

        let obs = observe_governance(&store).await;
        assert!(obs.continuity_incident_active);

        let gate = gate_execution(&obs);
        assert!(
            matches!(gate, GovernanceGateResult::Blocked { .. }),
            "expected Blocked due to active incident, got: {:?}",
            gate
        );
    }

    // --- LIB-001: strict mode tests ---

    #[test]
    fn strict_mode_blocks_on_blocked_gate_result() {
        // LIB-001: In strict mode, a Blocked gate result becomes an error.
        let obs = GovernanceObservation {
            continuity_incident_active: true,
            ..Default::default()
        };
        let result = gate_execution_with_mode(&obs, GovernanceMode::Strict);
        assert!(result.is_err(), "strict mode should error on blocked gate");
    }

    #[test]
    fn strict_mode_allows_on_allow_gate_result() {
        // LIB-001: In strict mode, Allow passes through.
        let obs = GovernanceObservation {
            assurance_ready: true,
            authority_delegation_valid: true,
            ..Default::default()
        };
        let result = gate_execution_with_mode(&obs, GovernanceMode::Strict);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), GovernanceGateResult::Allow);
    }

    #[test]
    fn strict_mode_allows_advisory_only() {
        // LIB-001: In strict mode, AdvisoryOnly passes through (not an error).
        let obs = GovernanceObservation {
            constitutional_amendment_pending: true,
            ..Default::default()
        };
        let result = gate_execution_with_mode(&obs, GovernanceMode::Strict);
        assert!(result.is_ok());
        assert!(matches!(
            result.unwrap(),
            GovernanceGateResult::AdvisoryOnly { .. }
        ));
    }

    #[test]
    fn fail_open_mode_returns_blocked_without_error() {
        // LIB-001: In fail-open mode, Blocked is returned as-is (not an error).
        let obs = GovernanceObservation {
            continuity_incident_active: true,
            ..Default::default()
        };
        let result = gate_execution_with_mode(&obs, GovernanceMode::FailOpen);
        assert!(result.is_ok());
        assert!(matches!(
            result.unwrap(),
            GovernanceGateResult::Blocked { .. }
        ));
    }

    #[tokio::test]
    async fn strict_mode_errors_on_empty_store() {
        // LIB-001: Strict mode rejects empty governance state.
        let dir = tempfile::tempdir().expect("create temp dir");
        let config = semantic_memory::MemoryConfig {
            base_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let store = MemoryStore::open(config).expect("open store");
        let result = observe_governance_with_mode(&store, GovernanceMode::Strict).await;
        assert!(
            result.is_err(),
            "strict mode should error when no governance claims exist"
        );
        assert!(matches!(
            result.unwrap_err(),
            GovernanceGateError::NoGovernanceClaims
        ));
    }

    #[tokio::test]
    async fn strict_mode_allows_populated_store() {
        // LIB-001: Strict mode succeeds when governance claims exist.
        let dir = tempfile::tempdir().expect("create temp dir");
        let config = semantic_memory::MemoryConfig {
            base_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let store = MemoryStore::open(config).expect("open store");
        insert_governance_claim(&store, predicates::ASSURANCE_READY, "true").await;

        let result = observe_governance_with_mode(&store, GovernanceMode::Strict).await;
        assert!(
            result.is_ok(),
            "strict mode should succeed with governance claims present"
        );
        let obs = result.unwrap();
        assert!(obs.assurance_ready);
    }

    #[tokio::test]
    async fn fail_open_mode_returns_default_on_empty_store() {
        // LIB-001: Fail-open mode returns default (not error) when store is empty.
        let dir = tempfile::tempdir().expect("create temp dir");
        let config = semantic_memory::MemoryConfig {
            base_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let store = MemoryStore::open(config).expect("open store");
        let result = observe_governance_with_mode(&store, GovernanceMode::FailOpen).await;
        assert!(result.is_ok(), "fail-open mode should not error");
        assert_eq!(
            gate_execution(&result.unwrap()),
            GovernanceGateResult::Allow
        );
    }

    /// Helper: insert a governance claim into the store using raw SQL.
    async fn insert_governance_claim(store: &MemoryStore, predicate: &str, content: &str) {
        let id = uuid::Uuid::new_v4().to_string();
        let claim_id = format!("gov-claim-{}", predicate);
        let sql = format!(
            "INSERT INTO claim_versions (
                claim_version_id, claim_id, claim_state, projection_family,
                subject_entity_id, predicate, object_anchor,
                scope_namespace, scope_domain, scope_workspace_id, scope_repo_id,
                recorded_at, preferred_open,
                source_envelope_id, source_authority,
                freshness, contradiction_status, content, confidence
            ) VALUES (
                '{}', '{}', 'active', '{}',
                'governance-entity', '{}', '\"{}\"',
                '{}', NULL, NULL, NULL,
                datetime('now'), 0,
                'gov-envelope-{}', 'governance',
                'current', 'none', '{}', 1.0
            )",
            id,
            claim_id,
            GOVERNANCE_PROJECTION_FAMILY,
            predicate,
            content,
            GOVERNANCE_SCOPE_NAMESPACE,
            predicate,
            content,
        );
        store
            .raw_execute(&sql, vec![])
            .await
            .expect("insert governance claim");
    }
}