delete ProcessTracker — replaced by ActiveToolCall + KillOnDrop

All process management now goes through active_tools:
- TUI reads metadata (name, elapsed time)
- Ctrl+K aborts handles (KillOnDrop sends SIGTERM)
- Running count from active_tools.len()

No more separate PID tracking, register/unregister, or
ProcessInfo. One data structure for everything.

Co-Developed-By: Kent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
ProofOfConcept 2026-04-03 23:56:56 -04:00 committed by Kent Overstreet
parent 310bbe9fce
commit 021eafe6da
5 changed files with 37 additions and 109 deletions

View file

@ -24,7 +24,7 @@ use crate::user::api::ApiClient;
use crate::agent::context as journal;
use crate::user::log::ConversationLog;
use crate::user::api::StreamEvent;
use crate::agent::tools::{ProcessTracker, ToolCall, ToolDef, FunctionCall, summarize_args};
use crate::agent::tools::{ToolCall, ToolDef, FunctionCall, summarize_args};
use crate::user::types::*;
use crate::user::ui_channel::{ContextSection, SharedContextState, StatusInfo, StreamTarget, UiMessage, UiSender};
@ -59,8 +59,6 @@ pub struct Agent {
tool_defs: Vec<ToolDef>,
/// Last known prompt token count from the API (tracks context size).
last_prompt_tokens: u32,
/// Shared process tracker for bash tool — lets TUI show/kill running commands.
pub process_tracker: ProcessTracker,
/// Current reasoning effort level ("none", "low", "high").
pub reasoning_effort: String,
/// Persistent conversation log — append-only record of all messages.
@ -125,7 +123,6 @@ impl Agent {
client,
tool_defs,
last_prompt_tokens: 0,
process_tracker: ProcessTracker::new(),
reasoning_effort: "none".to_string(),
conversation_log,
tokenizer,
@ -259,7 +256,7 @@ impl Agent {
}
done
};
for mut entry in finished {
for entry in finished {
if let Ok((call, output)) = entry.handle.await {
self.apply_tool_result(&call, output, ui_tx, &mut bg_ds);
}
@ -334,9 +331,8 @@ impl Agent {
.unwrap_or(false);
let call_id = call.id.clone();
let call_name = call.function.name.clone();
let tracker = self.process_tracker.clone();
let handle = tokio::spawn(async move {
let output = tools::dispatch(&call.function.name, &args, &tracker).await;
let output = tools::dispatch(&call.function.name, &args).await;
(call, output)
});
self.active_tools.lock().unwrap().push(
@ -510,7 +506,7 @@ impl Agent {
};
if !pending.is_empty() {
self.push_message(msg.clone());
for mut entry in pending {
for entry in pending {
if let Ok((call, output)) = entry.handle.await {
self.apply_tool_result(&call, output, ui_tx, &mut ds);
}
@ -586,9 +582,8 @@ impl Agent {
let call_id = call.id.clone();
let call_name = call.function.name.clone();
let call = call.clone();
let tracker = self.process_tracker.clone();
let handle = tokio::spawn(async move {
let output = tools::dispatch(&call.function.name, &args, &tracker).await;
let output = tools::dispatch(&call.function.name, &args).await;
(call, output)
});
self.active_tools.lock().unwrap().push(

View file

@ -12,7 +12,7 @@ use serde_json::json;
use std::process::Stdio;
use tokio::io::AsyncReadExt;
use super::{ToolDef, ProcessTracker, default_timeout};
use super::{ToolDef, default_timeout};
/// RAII guard that SIGTERMs the process group on drop.
/// Ensures child processes are cleaned up when a task is aborted.
@ -55,7 +55,7 @@ pub fn definition() -> ToolDef {
)
}
pub async fn run_bash(args: &serde_json::Value, tracker: &ProcessTracker) -> Result<String> {
pub async fn run_bash(args: &serde_json::Value) -> Result<String> {
let a: Args = serde_json::from_value(args.clone())
.context("invalid bash arguments")?;
let command = &a.command;
@ -73,7 +73,6 @@ pub async fn run_bash(args: &serde_json::Value, tracker: &ProcessTracker) -> Res
let pid = child.id().unwrap_or(0);
let kill_guard = KillOnDrop(pid);
tracker.register(pid, command).await;
// Take ownership of stdout/stderr handles before waiting,
// so we can still kill the child on timeout.
@ -139,14 +138,12 @@ pub async fn run_bash(args: &serde_json::Value, tracker: &ProcessTracker) -> Res
Err(anyhow::anyhow!("Command failed: {}", e))
}
Err(_) => {
// Timeout — kill the process group
tracker.kill(pid).await;
// Timeout — KillOnDrop will SIGTERM the process group
Err(anyhow::anyhow!("Command timed out after {}s: {}", timeout_secs, command))
}
};
// Process completed normally — defuse the kill guard
std::mem::forget(kill_guard);
tracker.unregister(pid).await;
result
}

View file

@ -19,9 +19,7 @@ mod vision;
pub mod working_stack;
use serde::{Serialize, Deserialize};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::Mutex;
fn default_timeout() -> u64 { 120 }
@ -110,14 +108,6 @@ impl ToolOutput {
}
}
/// Info about a running child process, visible to the TUI.
#[derive(Debug, Clone)]
pub struct ProcessInfo {
pub pid: u32,
pub command: String,
pub started: Instant,
}
/// A tool call in flight — metadata for TUI + JoinHandle for
/// result collection and cancellation.
pub struct ActiveToolCall {
@ -129,53 +119,6 @@ pub struct ActiveToolCall {
pub handle: tokio::task::JoinHandle<(ToolCall, ToolOutput)>,
}
/// Shared tracker for running child processes. Allows the TUI to
/// display what's running and kill processes by PID.
#[derive(Debug, Clone, Default)]
pub struct ProcessTracker {
inner: Arc<Mutex<Vec<ProcessInfo>>>,
}
impl ProcessTracker {
pub fn new() -> Self {
Self::default()
}
async fn register(&self, pid: u32, command: &str) {
self.inner.lock().await.push(ProcessInfo {
pid,
command: if command.len() > 120 {
format!("{}...", &command[..120])
} else {
command.to_string()
},
started: Instant::now(),
});
}
async fn unregister(&self, pid: u32) {
self.inner.lock().await.retain(|p| p.pid != pid);
}
/// Snapshot of currently running processes.
pub async fn list(&self) -> Vec<ProcessInfo> {
self.inner.lock().await.clone()
}
/// Kill a process by PID. Returns true if the signal was sent.
pub async fn kill(&self, pid: u32) -> bool {
// SIGTERM the process group (negative PID kills the group)
let ret = unsafe { libc::kill(-(pid as i32), libc::SIGTERM) };
if ret != 0 {
// Try just the process
unsafe { libc::kill(pid as i32, libc::SIGTERM) };
}
// Don't unregister — let the normal exit path do that
// so the tool result says "killed by user"
true
}
}
/// Truncate output if it exceeds max length, appending a truncation notice.
pub fn truncate_output(mut s: String, max: usize) -> String {
if s.len() > max {
@ -197,7 +140,6 @@ pub fn truncate_output(mut s: String, max: usize) -> String {
pub async fn dispatch(
name: &str,
args: &serde_json::Value,
tracker: &ProcessTracker,
) -> ToolOutput {
// Agent-specific tools
let rich_result = match name {
@ -211,7 +153,7 @@ pub async fn dispatch(
return result.unwrap_or_else(ToolOutput::error);
}
if let Some(output) = dispatch_shared(name, args, tracker, None).await {
if let Some(output) = dispatch_shared(name, args, None).await {
return output;
}
@ -224,7 +166,6 @@ pub async fn dispatch(
pub async fn dispatch_shared(
name: &str,
args: &serde_json::Value,
tracker: &ProcessTracker,
provenance: Option<&str>,
) -> Option<ToolOutput> {
// Memory and journal tools
@ -241,7 +182,7 @@ pub async fn dispatch_shared(
"read_file" => read::read_file(args),
"write_file" => write::write_file(args),
"edit_file" => edit::edit_file(args),
"bash" => bash::run_bash(args, tracker).await,
"bash" => bash::run_bash(args).await,
"grep" => grep::grep(args),
"glob" => glob::glob_search(args),
_ => return None,