feltdb 0.4.1

Embedded durable state store: an append-only log with atomic multi-record transactions, version-conditional writes, and idempotent retries
Documentation
//! Canonical FeltDB State Subsystem Public API Facade
//!
//! This module presents the graduated state model as a unified, product-facing
//! subsystem. Applications should use this facade to access FeltDB's state
//! primitives rather than reimplementing them.
//!
//! Proven Contracts:
//! - Deterministic content-addressed state identifiers
//! - Immutable revisions with explicit ancestry
//! - Durable persistence with restart recovery
//! - Causal topology tracking
//! - Semantic diff computation
//! - Conflict classification
//! - Explicit reconciliation (no automatic merge)

use crate::state_model::{
    ConflictClassification, ReconciliationPlan, Relationship, SemanticDiff, StateId,
    StateReconciliationResult, StateRevision, StateStore, StateTopology, STATE_MODEL_VERSION,
};
use crate::FeltDb;
use std::sync::Arc;

/// FeltDB State System - canonical state subsystem public API
pub struct FeltDBStateSystem;

impl FeltDBStateSystem {
    /// Create a new state store for an application with FeltDB persistence
    ///
    /// Returns a StateStore that provides:
    /// - Durable persistence through FeltDB's canonical operation log
    /// - Immutable revisions
    /// - Deterministic state identifiers
    /// - Restart recovery (once implemented)
    ///
    /// Applications MUST provide a FeltDb instance. This ensures all state
    /// mutations are persisted through FeltDB's canonical persistence boundary.
    ///
    /// # Arguments
    /// * `db` - Arc<FeltDb> instance for durable storage
    ///
    /// # Returns
    /// * `Ok(StateStore)` - Successfully initialized store with FeltDB backing
    /// * `Err(String)` - If initialization fails
    ///
    /// # Example
    /// ```ignore
    /// let db = FeltDb::open("./data")?;
    /// let store = FeltDBStateSystem::create_store(&Arc::new(db))?;
    /// let initial = store.create("docs:1", json_string, "app-authority")?;
    /// let same = store.get(&initial.id).expect("retrieved by identity");
    /// ```
    pub fn create_store(db: &Arc<FeltDb>) -> Result<StateStore, String> {
        StateStore::with_feltdb(db.clone())
    }

    /// Create a volatile (in-memory only) state store for testing
    ///
    /// This store is NOT persisted and will lose all data when dropped.
    /// This is test-only and should not be used in production.
    ///
    /// For production, use `create_store(&db)` which requires FeltDB persistence.
    pub fn create_test_store() -> StateStore {
        StateStore::new_volatile()
    }

    /// Get the version of the canonical state model
    pub fn version() -> u32 {
        STATE_MODEL_VERSION
    }

    /// Documentation: This is the canonical way to access FeltDB's state
    /// primitives. Do not reimplement StateId, StateRevision, StateHistory,
    /// StateStore, or related primitives.
    pub fn documentation() -> &'static str {
        r#"
        FeltDB State Subsystem - Canonical Primitives
        
        Applications should:
        1. Initialize FeltDB: let db = FeltDb::open(path)?;
        2. Use FeltDBStateSystem::create_store(&Arc::new(db)) to initialize state
        3. Call store.create() for initial state
        4. Call store.commit() for transitions
        5. Call store.get(&id) to retrieve a revision by identity
           (the store does not track which revision is "current" — see
            StateStore's own documentation for why that is a separate concern)
        6. Use StateTopology to inspect causal relationships
        7. Use SemanticDiff to compute changes between states
        8. Use ConflictClassification to analyze divergence
        9. Use ReconciliationPlan with explicit caller policy
        
        Applications should NOT:
        - Reimplement StateId
        - Reimplement StateRevision
        - Reimplement StateStore
        - Compute diffs independently
        - Implement their own conflict classification
        - Use implicit/automatic merge
        - Create StateStore without FeltDB backing (use new_volatile() for testing only)
        "#
    }
}

// Note: All types are re-exported at lib.rs level for public API

#[cfg(test)]
mod facade_tests {
    use super::*;
    use crate::{
        state_model::StateStore, ConflictClass, ConflictClassification, Relationship, SemanticDiff,
        StateTopology,
    };
    use serde_json::json;

    #[test]
    fn test_facade_creates_store() {
        // For testing, use new_volatile() - production must use create_store(&db)
        let store = StateStore::new_volatile();
        let initial = store
            .create(
                "docs:1",
                r#"{"data":"test"}"#.to_string(),
                "test-auth".to_string(),
            )
            .expect("Failed to create initial state");

        let stored = store
            .get(&initial.id)
            .expect("the revision is retrievable by identity");
        assert_eq!(stored.id, initial.id);
    }

    #[test]
    fn test_facade_version() {
        assert_eq!(FeltDBStateSystem::version(), 1);
    }

    #[test]
    fn test_facade_comprehensive_workflow() {
        // Initialize - for testing, use new_volatile()
        let store = StateStore::new_volatile();

        // Create initial state
        let initial = store
            .create(
                "docs:1",
                r#"{"users":{"alice":100}}"#.to_string(),
                "app-auth".to_string(),
            )
            .expect("Failed to create");

        // Verify the revision is retrievable by identity. The store tracks no
        // "current" pointer; the caller holds the ids it cares about.
        assert_eq!(store.get(&initial.id).unwrap().id, initial.id);

        // Divergence 1: alice updates
        let branch1 = store
            .commit(
                r#"{"users":{"alice":150}}"#.to_string(),
                &initial,
                "alice-auth".to_string(),
            )
            .expect("Failed branch 1");

        // Divergence 2: from the same parent
        let branch2 = store
            .commit(
                r#"{"users":{"alice":100,"bob":50}}"#.to_string(),
                &initial,
                "bob-auth".to_string(),
            )
            .expect("Failed branch 2");

        // Inspect topology
        let mut topology = StateTopology::new();
        topology.add_revision(initial.clone());
        topology.add_revision(branch1.clone());
        topology.add_revision(branch2.clone());

        // Verify relationships
        match topology.relationship(&branch1.id, &branch2.id) {
            Relationship::Diverged => {
                // Expected: both descended from initial but different
            }
            _ => panic!("Expected Diverged relationship"),
        }

        // Compute diff using JSON values
        let initial_json: serde_json::Value =
            serde_json::from_str(r#"{"users":{"alice":100}}"#).unwrap();
        let branch1_json: serde_json::Value =
            serde_json::from_str(r#"{"users":{"alice":150}}"#).unwrap();
        let diff = SemanticDiff::compute(&initial_json, &branch1_json);
        assert!(!diff.changes.is_empty());

        // Classify conflict
        let classification = ConflictClassification::classify(&initial, &branch1, &branch2);
        assert_eq!(classification.overall, ConflictClass::Independent);
    }

    #[test]
    fn test_facade_immutability() {
        let store = StateStore::new_volatile();
        let initial = store
            .create("docs:1", r#"{"v":1}"#.to_string(), "auth".to_string())
            .unwrap();

        let initial_id = initial.id.clone();

        // Create another state
        let second = store
            .commit(r#"{"v":2}"#.to_string(), &initial, "auth".to_string())
            .unwrap();

        // Verify first state is unchanged
        assert_eq!(store.get(&initial_id).unwrap().content, r#"{"v":1}"#);
        assert_eq!(initial_id, initial.id);
    }

    #[test]
    fn test_facade_restart_recovery() {
        let store1 = StateStore::new_volatile();
        let initial = store1
            .create(
                "docs:1",
                r#"{"data":"test"}"#.to_string(),
                "auth".to_string(),
            )
            .unwrap();

        // Simulate restart: new store instance
        let _store2 = StateStore::new_volatile();

        // Note: In real scenario, store would load from persistent storage
        // Here we demonstrate the API contract: states retrieved by id are valid
        assert!(store1.exists(&initial.id));
    }

    #[test]
    fn test_facade_authority_neutrality() {
        let store = StateStore::new_volatile();
        let initial = store
            .create(
                "docs:1",
                r#"{"balance":100}"#.to_string(),
                "alice".to_string(),
            )
            .unwrap();

        let update1 = store
            .commit(
                r#"{"balance":150}"#.to_string(),
                &initial,
                "alice".to_string(),
            )
            .unwrap();

        let update2 = store
            .commit(
                r#"{"balance":150}"#.to_string(),
                &initial,
                "bob".to_string(),
            )
            .unwrap();

        // The same state, from the same parent, is the same historical fact —
        // so it is one revision, whoever committed it. Authority is recorded
        // but takes no part in identity.
        assert_eq!(update1.id, update2.id);
        assert_eq!(update1.content_id, update2.content_id);

        // And because authority takes no part in identity, the second commit
        // is a no-op that returns what was already recorded. The authority on a
        // revision is whoever first committed that historical fact, not
        // whoever last asked for it.
        assert_eq!(update1.authority, "alice");
        assert_eq!(update2.authority, "alice");
    }

    #[test]
    fn test_facade_read_only_operations() {
        let store = StateStore::new_volatile();
        let initial = store
            .create("docs:1", r#"{"v":1}"#.to_string(), "auth".to_string())
            .unwrap();

        let update = store
            .commit(r#"{"v":2}"#.to_string(), &initial, "auth".to_string())
            .unwrap();

        // Topology operations should not mutate
        let mut topology = StateTopology::new();
        topology.add_revision(initial.clone());
        topology.add_revision(update.clone());

        let rel = topology.relationship(&initial.id, &update.id);
        assert!(matches!(rel, Relationship::Ancestor));

        // Verify both states still exist unchanged
        assert_eq!(store.get(&initial.id).unwrap().id, initial.id);
        assert_eq!(store.get(&update.id).unwrap().id, update.id);
    }

    #[test]
    fn test_facade_no_git_dependency() {
        // This test verifies we can use the state system without any .git access
        let store = StateStore::new_volatile();
        let state = store
            .create(
                "docs:1",
                r#"{"test":"no-git"}"#.to_string(),
                "auth".to_string(),
            )
            .expect("State creation should work without .git");

        // Topology and diff operations should work without git
        let mut topology = StateTopology::new();
        topology.add_revision(state.clone());
        assert!(topology.is_ancestor(&state.id, &state.id));
    }
}