clm-kernel 0.0.1

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use std::env;
use std::fs;

pub fn run_integration_internal_dispatch_zerofs() -> Result<String, String> {
    let (original_clm_root, test_dir) = super::setup_isolated_env("test_internal_dispatch_zerofs");

    println!("======================================================");
    println!("  Test Suite: Zero-FS Internal Rust Dispatch (100.2)");
    println!("======================================================\n");

    // Enable Zero-FS mode explicitly for this test
    crate::utils::set_env_var("CLM_ZERO_FS", "true");
    crate::utils::remove_env_var("ACTIVE_AGENT_DID"); // Clean start

    // 1. Initialize TriDB in-memory (using MemBroker snapshot directory)
    crate::db::init_tridb(test_dir.to_str().unwrap()).unwrap();
    println!("  ✅ init_tridb completed in-memory");

    // 2. Create a test CLM declaring runtime: rust + process.type: internal_reference
    let test_clm_path = test_dir.join("test_split_internal.yaml");
    let test_clm_content = r#"clm:
  abstract_spec:
    context: "Internal reference split verification"
    goal: "Verify split operation is dispatched internally"
    success_criteria: []
  concrete_impl:
    runtime: rust
    operation: split
    inputs:
      - clm_file: "string"
    outputs:
      - split_output: "string"
    process:
      type: internal_reference
      module: "clm_rust_core::baldwin::split"
  balanced_exp:
    expectation: "split completes"
"#;
    fs::write(&test_clm_path, test_clm_content).unwrap();

    // 3. Invoke internal dispatch
    println!("-- Invoking internal dispatch --");
    let exit_code = crate::evaluator::try_internal_dispatch(
        &test_dir,
        test_clm_path.to_str().unwrap(),
        None,
        None,  // post_rc
        false, // quiet = false to print verify blocks
    ).map_err(|e| format!("try_internal_dispatch failed: {}", e))?;

    assert_eq!(exit_code, 0);
    println!("  ✅ Internal dispatch executed successfully (exit_code=0)");

    // 4. Verify that VCard Pre-Gate created card & registry values in agent_identities.db
    let identities_db_path = crate::db::get_db_path("agent_identities", test_dir.to_str().unwrap());
    let conn_id = rusqlite::Connection::open(&identities_db_path).unwrap();
    
    let active_did = env::var("ACTIVE_AGENT_DID").unwrap();
    let root_card_exists: i64 = conn_id.query_row(
        "SELECT count(*) FROM card WHERE hash = ?;",
        [&active_did],
        |row| row.get(0)
    ).unwrap_or(0);
    assert_eq!(root_card_exists, 1);
    println!("  ✅ Root agent DID card registered in agent_identities DB");

    // 5. Verify that VCard Post-Gate created card record in execution_log.db
    let log_db_path = crate::db::get_db_path("execution_log", test_dir.to_str().unwrap());
    let conn_log = rusqlite::Connection::open(&log_db_path).unwrap();
    let log_count: i64 = conn_log.query_row(
        "SELECT count(*) FROM card;",
        [],
        |row| row.get(0)
    ).unwrap_or(0);
    assert_eq!(log_count, 3); // 1 schema + 2 execution logs (operation execution + post-gate)
    println!("  ✅ VCard Sandwich Post-Gate successfully registered execution card in log DB");

    // Restore env vars
    crate::utils::remove_env_var("CLM_ZERO_FS");
    super::restore_env(&original_clm_root, &test_dir);
    println!("\n======================================================");
    print!("  Results: 5 passed, 0 failed (of 5)");
    Ok("".to_string())
}