use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use pensieve_brain::gitbin::GitBin;
use pensieve_brain::registry::BrainRegistry;
use crate::agent::state::AgentState;
pub mod fetch;
pub mod git_http;
pub mod pg_registry;
pub mod routes;
pub mod scheduler;
#[derive(Clone)]
pub struct BrainState {
pub registry: Arc<dyn BrainRegistry>,
pub git: Option<Arc<GitBin>>,
pub brain_dir: PathBuf,
pub locks: Arc<Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>>,
pub agent: AgentState,
}
impl BrainState {
pub fn new(
registry: Arc<dyn BrainRegistry>,
git: Option<Arc<GitBin>>,
brain_dir: PathBuf,
agent: AgentState,
) -> Self {
Self { registry, git, brain_dir, locks: Arc::new(Mutex::new(HashMap::new())), agent }
}
pub fn lock_for(&self, name: &str) -> Arc<tokio::sync::Mutex<()>> {
let mut map = self.locks.lock().expect("brain lock map poisoned");
map.entry(name.to_string())
.or_insert_with(|| Arc::new(tokio::sync::Mutex::new(())))
.clone()
}
pub fn repo_dir(&self, name: &str) -> PathBuf {
self.brain_dir.join(format!("{name}.git"))
}
}