diff --git a/crates/openfang-kernel/src/kernel.rs b/crates/openfang-kernel/src/kernel.rs index 5e582d048..ca96e4602 100644 --- a/crates/openfang-kernel/src/kernel.rs +++ b/crates/openfang-kernel/src/kernel.rs @@ -5715,6 +5715,12 @@ impl KernelHandle for OpenFangKernel { .collect() } + fn touch_agent(&self, agent_id: &str) { + if let Ok(id) = agent_id.parse::() { + self.registry.touch(id); + } + } + fn kill_agent(&self, agent_id: &str) -> Result<(), String> { let id: AgentId = agent_id .parse() diff --git a/crates/openfang-kernel/src/registry.rs b/crates/openfang-kernel/src/registry.rs index b3c3a4962..0852a658e 100644 --- a/crates/openfang-kernel/src/registry.rs +++ b/crates/openfang-kernel/src/registry.rs @@ -235,6 +235,14 @@ impl AgentRegistry { Ok(()) } + /// Touch an agent — refresh last_active without changing any other state. + /// Used by the agent loop to prevent heartbeat false-positives during long LLM calls. + pub fn touch(&self, id: AgentId) { + if let Some(mut entry) = self.agents.get_mut(&id) { + entry.last_active = chrono::Utc::now(); + } + } + /// Update an agent's system prompt (hot-swap, takes effect on next message). pub fn update_system_prompt(&self, id: AgentId, new_prompt: String) -> OpenFangResult<()> { let mut entry = self diff --git a/crates/openfang-runtime/src/agent_loop.rs b/crates/openfang-runtime/src/agent_loop.rs index 2fd481d68..9830b6287 100644 --- a/crates/openfang-runtime/src/agent_loop.rs +++ b/crates/openfang-runtime/src/agent_loop.rs @@ -348,6 +348,12 @@ pub async fn run_agent_loop( cb(LoopPhase::Thinking); } + // Stamp last_active before the (potentially long) LLM call so the + // heartbeat monitor doesn't flag us as unresponsive mid-iteration. + if let Some(k) = &kernel { + k.touch_agent(&agent_id_str); + } + // Call LLM with retry, error classification, and circuit breaker let provider_name = manifest.model.provider.as_str(); let mut response = call_with_retry(&*driver, request, Some(provider_name), None).await?; diff --git a/crates/openfang-runtime/src/kernel_handle.rs b/crates/openfang-runtime/src/kernel_handle.rs index 00f195599..e3e1b7633 100644 --- a/crates/openfang-runtime/src/kernel_handle.rs +++ b/crates/openfang-runtime/src/kernel_handle.rs @@ -238,6 +238,12 @@ pub trait KernelHandle: Send + Sync { Err("Channel file data send not available".to_string()) } + /// Refresh an agent's last_active timestamp without changing any other state. + /// Called by the agent loop before long LLM calls to prevent heartbeat false-positives. + fn touch_agent(&self, agent_id: &str) { + let _ = agent_id; + } + /// Spawn an agent with capability inheritance enforcement. /// `parent_caps` are the parent's granted capabilities. The kernel MUST verify /// that every capability in the child manifest is covered by `parent_caps`.