use serde_json::Value;
use std::fs;
use std::io::{self, Read};
use std::path::Path;
use crate::db::{card_exists, handle_register_batch, resolve_handle_hash};
use crate::fs_utils::{compute_content_hash, compute_stripped_file_hash, get_file_handle};
use crate::yaml::{parse_to_mcards, evaluate_jq};
use crate::classifier::{classify, detect_dialect};
use crate::cache::setter_mcard_cache_handle;
pub fn memoize_clm_state(
_file_path: &str,
content_hash: &str,
json_ast: &str,
clm_root: &str,
) -> Result<(), String> {
let parsed: Value = serde_json::from_str(json_ast)
.map_err(|e| format!("Invalid JSON AST: {}", e))?;
let classification = classify(&parsed);
let dialect = detect_dialect(&parsed, &classification);
let items = vec![
("ast", json_ast.to_string()),
("classification", classification),
("dialect", dialect),
];
for (key, val) in items {
let val_hash = compute_content_hash(&val);
let handle_name = format!("{}:{}", key, content_hash);
handle_register_batch(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &handle_name, &val_hash, &val)
.map_err(|e| format!("Failed to register handle {}: {}", handle_name, e))?;
setter_mcard_cache_handle(&handle_name, &val_hash);
}
Ok(())
}
pub fn check_and_register_file_mcard(file_path: &str, clm_root: &str) -> Result<(), String> {
let path = Path::new(file_path);
if !path.is_file() {
return Err(format!("Not a file: {}", file_path));
}
let handle_name = get_file_handle(file_path, Some(clm_root));
let content_hash = compute_stripped_file_hash(path)
.map_err(|e| format!("Failed to compute file hash: {}", e))?;
let existing_hash = resolve_handle_hash(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &handle_name)
.unwrap_or(None);
if existing_hash.is_none() || existing_hash.as_ref().unwrap() != &content_hash {
let raw_content = fs::read_to_string(path)
.map_err(|e| format!("Failed to read file: {}", e))?;
let content = raw_content.trim_end_matches(['\n', '\r']).to_string();
handle_register_batch(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &handle_name, &content_hash, &content)
.map_err(|e| format!("Failed to register handle {}: {}", handle_name, e))?;
if existing_hash.is_none() {
eprintln!(
"[MCARD_KERNEL] 🆕 Registered physical file MCard handle: {} -> {}",
handle_name, content_hash
);
} else {
eprintln!(
"[MCARD_KERNEL] 🔄 Change detected in physical file! Updated handle: {} ({} -> {})",
handle_name,
existing_hash.unwrap(),
content_hash
);
}
match parse_to_mcards(file_path) {
Ok(json_ast) => {
if let Err(e) = memoize_clm_state(file_path, &content_hash, &json_ast, clm_root) {
eprintln!(
"[MCARD_KERNEL] ⚠️ Failed to memoize CLM state for {}: {}",
file_path, e
);
}
}
Err(e) => {
eprintln!(
"[MCARD_KERNEL] ⚠️ Failed to parse CLM AST for memoization: {} (File registered successfully without AST caching)",
e
);
}
}
}
Ok(())
}
pub fn memorize_mcard_bytes(
raw_bytes: &[u8],
pillar: &str,
clm_root: &str,
base_filename: Option<&str>,
) -> Result<i32, String> {
let data_root = Path::new(clm_root).join("data");
let is_sandbox = crate::db::TEST_SANDBOX_ACTIVE.with(|active| active.get());
let zero_fs = is_sandbox || crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_ZERO_FS).unwrap_or_default() == "true";
let mcard_content_str = std::str::from_utf8(raw_bytes).ok();
let parsed_yaml: Option<Value> = mcard_content_str.and_then(|s| serde_yaml::from_str(s).ok());
let record = crate::layer0::mcard_record::MCardRecord::create(raw_bytes, None)?;
let content_hash_str = record.hash;
let g_time_str = record.g_time;
let target_dir = match pillar {
"identity" | "id" | crate::layer0::vocabulary_env::PILLAR_AGENT_IDENTITIES => data_root.join("agent_identity"),
"memory" | "mem" | "content" | crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY => data_root.join(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY),
"log" | "exec" | "execution" | crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG => data_root.join("execution_logs"),
"auto" => {
let src_fn_str = parsed_yaml.as_ref()
.map(|y| evaluate_jq(y, ".mcard.source.filename"))
.and_then(|v| match v { Value::String(s) => Some(s), _ => None })
.unwrap_or_default();
let basename_file = base_filename.unwrap_or("").to_string();
let computed_dir = if basename_file.starts_with("exec_") {
data_root.join("execution_logs")
} else if src_fn_str.ends_with(".pem")
|| src_fn_str.ends_with(".key")
|| src_fn_str.ends_with(".cert")
|| src_fn_str.ends_with(".crt")
|| src_fn_str.ends_with(".pub")
|| src_fn_str.ends_with(".sig")
|| src_fn_str.ends_with(".did")
{
data_root.join("agent_identity")
} else if !src_fn_str.is_empty() && src_fn_str != "null" {
data_root.join(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY)
} else {
data_root.join("execution_logs")
};
eprintln!(
"[MEMORIZE] Auto-classified pillar: {}",
computed_dir.file_name().unwrap().to_string_lossy()
);
computed_dir
}
_ => {
return Err(format!(
"[MEMORIZE] ❌ Unknown pillar: {} (use: identity, memory, log, or auto)",
pillar
))
}
};
if !zero_fs {
let _ = fs::create_dir_all(&target_dir);
}
let pillar_name = target_dir.file_name().unwrap().to_string_lossy().to_string();
let db_name = match pillar_name.as_str() {
"agent_identity" => crate::layer0::vocabulary_env::PILLAR_AGENT_IDENTITIES,
crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY => crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY,
"execution_logs" => crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG,
_ => crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY,
};
let short_hash = if content_hash_str.len() >= 8 {
&content_hash_str[0..8]
} else {
&content_hash_str
};
let mut existing_db = false;
if card_exists(db_name, clm_root, &content_hash_str).map_err(|error| error.to_string())? {
crate::db::setter_card_bytes_batch(db_name, clm_root, &content_hash_str, raw_bytes, &g_time_str)
.map_err(|error| error.to_string())?;
existing_db = true;
}
let mut existing_file = String::new();
if !zero_fs && target_dir.exists()
&& let Ok(entries) = fs::read_dir(&target_dir) {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.contains(short_hash) && (name.ends_with(".yaml") || name.ends_with(".dat")) {
existing_file = entry.path().to_string_lossy().to_string();
break;
}
}
}
if existing_db {
eprintln!("[MEMORIZE] ⏭️ Duplicate detected — already memorized");
if !existing_file.is_empty() {
eprintln!("[MEMORIZE] existing: {}", existing_file);
}
if existing_db {
eprintln!("[MEMORIZE] db_check: found in {}.db", db_name);
}
eprintln!("[MEMORIZE] pillar: {}", pillar_name);
eprintln!("[MEMORIZE] content_hash: {}", content_hash_str);
return Ok(2);
}
let unix_ts = chrono::Utc::now().timestamp();
let ext = if parsed_yaml.is_some() { "yaml" } else { "dat" };
let target_file = target_dir.join(format!("mcard_{}_{}.{}", unix_ts, short_hash, ext));
if !zero_fs {
fs::write(&target_file, raw_bytes)
.map_err(|e| format!("[MEMORIZE] ❌ Failed to store MCard: {}", e))?;
}
crate::db::setter_card_bytes_batch(db_name, clm_root, &content_hash_str, raw_bytes, &g_time_str)
.map_err(|e| format!("Failed to write to database: {}", e))?;
eprintln!("[MEMORIZE] db_write: {}", db_name);
eprintln!("[MEMORIZE] ✅ MCard memorized");
eprintln!("[MEMORIZE] pillar: {}", pillar_name);
if !zero_fs {
eprintln!("[MEMORIZE] stored_at: {}", target_file.display());
}
eprintln!("[MEMORIZE] content_hash: {}", content_hash_str);
eprintln!("[MEMORIZE] g_time: {}", g_time_str);
Ok(0)
}
pub fn memorize_mcard_string(
mcard_content_str: &str,
pillar: &str,
clm_root: &str,
base_filename: Option<&str>,
) -> Result<i32, String> {
memorize_mcard_bytes(
mcard_content_str.as_bytes(),
pillar,
clm_root,
base_filename,
)
}
pub fn memorize_mcard(mcard_file: &str, pillar: &str, clm_root: &str) -> Result<i32, String> {
let raw_bytes = if mcard_file == "/dev/stdin" || mcard_file == "-" {
let mut buffer = Vec::new();
let mut stdin = io::stdin();
stdin.read_to_end(&mut buffer).map_err(|e| e.to_string())?;
buffer
} else {
let path = Path::new(mcard_file);
if !path.is_file() {
return Err(format!("[MEMORIZE] ❌ MCard file not found: {}", mcard_file));
}
fs::read(path).map_err(|e| e.to_string())?
};
let basename_file = Path::new(mcard_file)
.file_name()
.map(|f| f.to_string_lossy().to_string());
memorize_mcard_bytes(
&raw_bytes,
pillar,
clm_root,
basename_file.as_deref(),
)
}