use chrono::Utc;
use super::resource_broker::{
MUTATING_CAPACITY_MARKER, READ_ONLY_PRESENCE_ROLE, consumes_worker_capacity,
ensure_worker_capacity, has_active_worker_lease, max_concurrent_mutating_workers,
max_concurrent_workers, mutating_capacity_message, registration_consumes_capacity,
role_can_mutate,
};
use super::{
AgentEntry, AgentRegistry, AgentStatus, ProcessIdentityIndex, mutate_persistent, presence_ttl,
process_identity_matches,
};
impl AgentRegistry {
pub(super) fn ensure_registration_capacity(
&self,
agent_type: &str,
role: Option<&str>,
project_root: &str,
exclude_agent_id: Option<&str>,
compatibility_identities: &ProcessIdentityIndex,
now: chrono::DateTime<Utc>,
) -> Result<(), String> {
if !registration_consumes_capacity(agent_type, role) {
return Ok(());
}
let is_counted = |agent: &AgentEntry| {
exclude_agent_id.is_none_or(|own| agent.agent_id != own)
&& consumes_worker_capacity(agent)
&& has_active_worker_lease(agent, now)
&& process_identity_matches(agent, compatibility_identities)
};
let active_machine_wide = self.agents.iter().filter(|a| is_counted(a)).count();
ensure_worker_capacity(active_machine_wide, max_concurrent_workers(), project_root)?;
if role_can_mutate(role) {
let active_mutating = self
.agents
.iter()
.filter(|agent| is_counted(agent) && role_can_mutate(agent.role.as_deref()))
.count();
let limit = max_concurrent_mutating_workers();
if active_mutating >= limit {
return Err(mutating_capacity_message(active_mutating, limit));
}
}
if let Some(role) = role.map(str::trim).filter(|role| !role.is_empty())
&& role != READ_ONLY_PRESENCE_ROLE
&& self.agents.iter().any(|agent| {
exclude_agent_id.is_none_or(|own| agent.agent_id != own)
&& agent.project_root == project_root
&& agent
.role
.as_deref()
.is_some_and(|active| active.eq_ignore_ascii_case(role))
&& has_active_worker_lease(agent, now)
&& process_identity_matches(agent, compatibility_identities)
})
{
return Err(format!(
"duplicate active role rejected for {project_root}: {role}; reuse or finish the existing worker"
));
}
Ok(())
}
pub(crate) fn register_mcp_process(project_root: &str) -> Result<String, String> {
Self::register_mcp_process_as(project_root, "context-engine")
}
pub(crate) fn register_mcp_process_as(
project_root: &str,
role: &str,
) -> Result<String, String> {
mutate_persistent(|registry| {
registry.cleanup_stale(presence_ttl());
registry.register("mcp", Some(role), project_root)
})
.and_then(|result| result)
}
pub(crate) fn admit_read_only_presence(
project_root: &str,
requested_role: &str,
) -> Result<String, String> {
mutate_persistent(|registry| {
registry.cleanup_stale(presence_ttl());
let agent_id =
registry.register("mcp", Some(READ_ONLY_PRESENCE_ROLE), project_root)?;
registry.set_status(
&agent_id,
AgentStatus::Active,
Some(&format!(
"admitted read-only: {MUTATING_CAPACITY_MARKER} while registering as {requested_role}"
)),
)?;
Ok(agent_id)
})
.and_then(|result| result)
}
pub(crate) fn is_mutating_capacity_error(error: &str) -> bool {
error.contains(MUTATING_CAPACITY_MARKER)
}
}
#[cfg(all(test, unix))]
mod tests {
use chrono::Utc;
use super::super::{AgentEntry, AgentRegistry, AgentStatus, ProcessIdentityIndex};
fn registry_with_full_mutating_cap() -> (
AgentRegistry,
crate::core::data_dir::IsolatedDataDir,
tempfile::TempDir,
) {
let isolated = crate::core::data_dir::isolated_data_dir();
let config_dir = tempfile::tempdir().expect("config dir");
std::fs::write(
config_dir.path().join("config.toml"),
"[agents]\nmax_concurrent_mutating_workers = 1\n",
)
.expect("write config");
crate::test_env::set_var("LEAN_CTX_CONFIG_DIR", config_dir.path());
let filler_pid = std::os::unix::process::parent_id();
let now = Utc::now();
let mut registry = AgentRegistry::new();
registry.agents.push(AgentEntry {
agent_id: "cursor-filler".to_string(),
agent_type: "cursor".to_string(),
role: Some("coder".to_string()),
project_root: "/elsewhere".to_string(),
started_at: now,
last_active: now,
pid: filler_pid,
process_identity: crate::ipc::process::identity(filler_pid),
status: AgentStatus::Active,
status_message: None,
});
assert!(
registry.agents[0].process_identity.is_some(),
"the parent process must have a readable identity"
);
(registry, isolated, config_dir)
}
fn role_of<'a>(registry: &'a AgentRegistry, agent_id: &str) -> Option<&'a str> {
registry
.agents
.iter()
.find(|agent| agent.agent_id == agent_id)
.and_then(|agent| agent.role.as_deref())
}
#[test]
fn a_mutating_role_over_the_cap_is_refused_with_the_marker() {
let (mut registry, _iso, _cfg) = registry_with_full_mutating_cap();
let error = registry
.register("mcp", Some("coder"), "/project")
.expect_err("the mutating cap is full");
assert!(AgentRegistry::is_mutating_capacity_error(&error), "{error}");
assert!(error.contains("1/1"), "{error}");
assert!(
error.contains("admitted read-only"),
"the message must name the recovery path: {error}"
);
crate::test_env::remove_var("LEAN_CTX_CONFIG_DIR");
}
#[test]
fn a_non_consuming_presence_is_never_refused_by_a_full_cap() {
let (mut registry, _iso, _cfg) = registry_with_full_mutating_cap();
registry
.register("mcp", Some("context-engine"), "/project")
.expect("a placeholder presence is admitted");
crate::test_env::remove_var("LEAN_CTX_CONFIG_DIR");
}
#[test]
fn a_read_only_admission_persists_the_requested_role_in_its_status() {
let (registry, _iso, _cfg) = registry_with_full_mutating_cap();
registry.save().expect("persist the filler");
let id = AgentRegistry::admit_read_only_presence("/project", "coder")
.expect("read-only takes no mutating slot");
let loaded = AgentRegistry::load().expect("registry");
let me = loaded
.agents
.iter()
.find(|agent| agent.agent_id == id)
.expect("the presence is on disk");
assert_eq!(me.role.as_deref(), Some("read-only"));
assert!(
me.status_message
.as_deref()
.unwrap_or_default()
.contains("coder"),
"the status must say which role was requested: {:?}",
me.status_message
);
loaded
.ensure_registration_capacity(
"mcp",
Some("read-only"),
"/project",
None,
&ProcessIdentityIndex::load(),
Utc::now(),
)
.expect("read-only is never a duplicate role");
let mut freed = loaded;
freed
.agents
.iter_mut()
.find(|agent| agent.agent_id == "cursor-filler")
.expect("filler")
.status = AgentStatus::Finished;
freed.save().expect("persist the freed slot");
let upgraded = AgentRegistry::register_mcp_process_as("/project", "coder")
.expect("the slot is free now");
assert_eq!(upgraded, id, "same record, not a duplicate");
let after = AgentRegistry::load().expect("registry");
let me = after
.agents
.iter()
.find(|agent| agent.agent_id == id)
.expect("the presence is on disk");
assert_eq!(me.role.as_deref(), Some("coder"));
assert_eq!(
me.status_message, None,
"a role change clears the role-bound admission note"
);
crate::test_env::remove_var("LEAN_CTX_CONFIG_DIR");
}
#[test]
fn upgrading_a_placeholder_to_a_mutating_role_is_checked_against_the_cap() {
let (mut registry, _iso, _cfg) = registry_with_full_mutating_cap();
let placeholder = registry
.register("mcp", Some("context-engine"), "/project")
.expect("placeholder");
let error = registry
.register("mcp", Some("coder"), "/project")
.expect_err("the upgrade is over the cap");
assert!(AgentRegistry::is_mutating_capacity_error(&error), "{error}");
assert_eq!(
role_of(®istry, &placeholder),
Some("context-engine"),
"a refused upgrade leaves the record unchanged"
);
registry.agents[0].status = AgentStatus::Finished;
let upgraded = registry
.register("mcp", Some("coder"), "/project")
.expect("the slot is free now");
assert_eq!(upgraded, placeholder, "same record, not a duplicate");
assert_eq!(role_of(®istry, &placeholder), Some("coder"));
crate::test_env::remove_var("LEAN_CTX_CONFIG_DIR");
}
#[test]
fn the_placeholder_retry_never_overwrites_an_explicit_role() {
let _iso = crate::core::data_dir::isolated_data_dir();
let mut registry = AgentRegistry::new();
let id = registry
.register("mcp", Some("reviewer"), "/project")
.expect("reviewer");
let again = registry
.register("mcp", Some("context-engine"), "/project")
.expect("retry for the same process");
assert_eq!(id, again);
assert_eq!(role_of(®istry, &id), Some("reviewer"));
let mut fresh = AgentRegistry::new();
let placeholder = fresh
.register("mcp", Some("context-engine"), "/project")
.expect("placeholder");
fresh
.register("mcp", Some("debugger"), "/project")
.expect("upgrade");
assert_eq!(role_of(&fresh, &placeholder), Some("debugger"));
}
}